0

如何在android中创建一个Sqlite数据库?在这段代码中,我得到了一个错误。

package com.ms.adpterDB;

import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class Friend_listadpter {
     public static final String KEY_ROWID = "_id";

    public static final String KEY_NAME = "name";
    public static final String KEY_NNAME = "nickname";
    public static final String KEY_BIRTHDAY = "birthday";
    public static final String KEY_HOBBIES = "hobbies";
    public static final String KEY_PHONENO = "phoneno";
    public static final String KEY_EMAILID = "email";
    public static final String KEY_LINS = "lines";
    public static final String KEY_image = "image";


    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "Mobileslam";
    private static final String DATABASE_TABLE = "Friends";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table Friends(_id integer primary key autoincrement, "+ "name text not null, nickname text not null, " 
        + "birthday text not null,hobbies text not null,phoneno text not null,email text not null,lines text not null,image text not null);";

    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db; 

    public  Friend_listadpter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }    

    //---opens the database---
    public  Friend_listadpter open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }

    //---insert a title into the database---
    public long insertTitle(long _id,String name, String nickname, String birthday, String hobbies,
            String phoneno, String email, String lines, String image) 
    {
        ContentValues initialValues = new ContentValues();
      //  initialValues.put(KEY_ROWID, _id);


        initialValues.put(KEY_NAME, name);
        initialValues.put(KEY_NNAME, nickname);
        initialValues.put(KEY_BIRTHDAY,birthday);
        initialValues.put(KEY_HOBBIES,hobbies);
        initialValues.put(KEY_PHONENO,phoneno);
        initialValues.put(KEY_EMAILID,email);
        initialValues.put(KEY_LINS,lines);
        initialValues.put(KEY_image,image);
        initialValues.put(KEY_ROWID,_id);
        //initialValues.put(KEY_time,date.getDate());        
        Log.w(TAG, "inserted friend" + name+"____"+nickname+"____"+birthday+"____"+hobbies+"____"+phoneno+"____"+email+"____"+lines+"____"+image);
        db.execSQL("DROP TABLE IF EXISTS titles");
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    //---deletes a particular title---
    public boolean deleteTitle(long rowId) 
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + 
                "=" + rowId, null) > 0;
    }






    public Cursor getAllTitles() 
    { Log.w(TAG, "//////// GET ALL TITLES ////////" );
        return db.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, 
                KEY_NAME,
                KEY_NNAME,
                KEY_BIRTHDAY,
                KEY_HOBBIES,
                KEY_PHONENO,
                KEY_EMAILID,
                KEY_LINS,
                KEY_image,
               }, 
                null, 
                null, 
                null, 
                null, 
                null);
    }

    //---retrieves a particular title---
    public Cursor getTitle(long rowId) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {
                        KEY_ROWID,
                        KEY_NAME,
                        KEY_NNAME,
                        KEY_BIRTHDAY,
                        KEY_HOBBIES,
                        KEY_PHONENO,
                        KEY_EMAILID,
                        KEY_LINS,
                        KEY_BIRTHDAY,
                        }, 
                        KEY_ROWID + "=" + rowId, 
                        null,
                        null, 
                        null, 
                        null, 
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---updates a title---
    public boolean updateTitle(long _id, String name, 
    String nickname, String birthday, String hobbies, String phoneno,
    String email, String lines, String image) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_ROWID, _id);
        args.put(KEY_NAME, name);
        args.put(KEY_NNAME, nickname);
        args.put(KEY_BIRTHDAY,birthday);
        args.put(KEY_HOBBIES,hobbies);
        args.put(KEY_PHONENO,phoneno);
        args.put(KEY_EMAILID,email);
        args.put(KEY_LINS,lines);
        args.put(KEY_BIRTHDAY,image);



        return db.update(DATABASE_TABLE, args, 
                         KEY_ROWID , null) > 0;
    }


}

错误:

04-13 12:30:15.683: D/dalvikvm(336): GC_EXTERNAL_ALLOC freed 49K, 53% free 2551K/5379K, external 1625K/2137K, paused 76ms
04-13 12:30:15.863: D/dalvikvm(336): GC_EXTERNAL_ALLOC freed 2K, 53% free 2556K/5379K, external 2325K/2779K, paused 57ms
04-13 12:31:13.512: W/KeyCharacterMap(336): No keyboard for id 0
04-13 12:31:13.522: W/KeyCharacterMap(336): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
04-13 12:31:22.142: W/IInputConnectionWrapper(336): showStatusIcon on inactive InputConnection
04-13 12:31:24.203: D/(336): strUrl: file:///mnt/sdcard/DCIM/Camera/1334074966132.jpg
04-13 12:31:34.343: W/DBAdapter(336): inserted friendsravanthi____chinnu____july16th____xxxxx____908756889____chinni.sravs____mmmmm____file:///mnt/sdcard/DCIM/Camera/1334074966132.jpg
04-13 12:31:34.374: E/Database(336): Error inserting birthday=july16th _id=1 email=chinni.sravs nickname=chinnu name=sravanthi image=file:///mnt/sdcard/DCIM/Camera/1334074966132.jpg lines=mmmmm hobbies=xxxxx phoneno=908756889
04-13 12:31:34.374: E/Database(336): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
04-13 12:31:34.374: E/Database(336):    at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
04-13 12:31:34.374: E/Database(336):    at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
04-13 12:31:34.374: E/Database(336):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1582)
04-13 12:31:34.374: E/Database(336):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
04-13 12:31:34.374: E/Database(336):    at com.ms.adpterDB.Friend_listadpter.insertTitle(Friend_listadpter.java:106)
04-13 12:31:34.374: E/Database(336):    at com.slam.Add_friend.onClick(Add_friend.java:66)
04-13 12:31:34.374: E/Database(336):    at android.view.View.performClick(View.java:2485)
04-13 12:31:34.374: E/Database(336):    at android.view.View$PerformClick.run(View.java:9080)
04-13 12:31:34.374: E/Database(336):    at android.os.Handler.handleCallback(Handler.java:587)
04-13 12:31:34.374: E/Database(336):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 12:31:34.374: E/Database(336):    at android.os.Looper.loop(Looper.java:123)
04-13 12:31:34.374: E/Database(336):    at android.app.ActivityThread.main(ActivityThread.java:3683)
04-13 12:31:34.374: E/Database(336):    at java.lang.reflect.Method.invokeNative(Native Method)
04-13 12:31:34.374: E/Database(336):    at java.lang.reflect.Method.invoke(Method.java:507)
04-13 12:31:34.374: E/Database(336):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-13 12:31:34.374: E/Database(336):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-13 12:31:34.374: E/Database(336):    at dalvik.system.NativeStart.main(Native Method)
04-13 12:31:34.693: D/dalvikvm(336): GC_EXTERNAL_ALLOC freed 480K, 54% free 2711K/5831K, external 3962K/4199K, paused 67ms
04-13 12:31:34.863: D/dalvikvm(336): GC_EXTERNAL_ALLOC freed 36K, 55% free 2681K/5831K, external 4644K/5675K, paused 57ms
04-13 12:31:37.352: D/AndroidRuntime(336): Shutting down VM
04-13 12:31:37.352: W/dalvikvm(336): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-13 12:31:37.374: E/AndroidRuntime(336): FATAL EXCEPTION: main
04-13 12:31:37.374: E/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.slam/com.slam.Friend_List}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-13 12:31:37.374: E/AndroidRuntime(336):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-13 12:31:37.374: E/AndroidRuntime(336):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-13 12:31:37.374: E/AndroidRuntime(336):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-13 12:31:37.374: E/AndroidRuntime(336):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-13 12:31:37.374: E/AndroidRuntime(336):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 12:31:37.374: E/AndroidRuntime(336):  at android.os.Looper.loop(Looper.java:123)
04-13 12:31:37.374: E/AndroidRuntime(336):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-13 12:31:37.374: E/AndroidRuntime(336):  at java.lang.reflect.Method.invokeNative(Native Method)
04-13 12:31:37.374: E/AndroidRuntime(336):  at java.lang.reflect.Method.invoke(Method.java:507)
04-13 12:31:37.374: E/AndroidRuntime(336):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-13 12:31:37.374: E/AndroidRuntime(336):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-13 12:31:37.374: E/AndroidRuntime(336):  at dalvik.system.NativeStart.main(Native Method)
04-13 12:31:37.374: E/AndroidRuntime(336): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-13 12:31:37.374: E/AndroidRuntime(336):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
04-13 12:31:37.374: E/AndroidRuntime(336):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
04-13 12:31:37.374: E/AndroidRuntime(336):  at android.app.Activity.setContentView(Activity.java:1657)
04-13 12:31:37.374: E/AndroidRuntime(336):  at com.slam.Friend_List.onCreate(Friend_List.java:27)
04-13 12:31:37.374: E/AndroidRuntime(336):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-13 12:31:37.374: E/AndroidRuntime(336):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-13 12:31:37.374: E/AndroidRuntime(336):  ... 11 more
4

1 回答 1

0

我建议你看看这两行:

04-13 12:31:34.374: E/Database(336): Error inserting birthday=july16th _id=1 email=chinni.sravs nickname=chinnu name=sravanthi image=file:///mnt/sdcard/DCIM/Camera/1334074966132.jpg lines=mmmmm hobbies=xxxxx phoneno=908756889
04-13 12:31:34.374: E/Database(336): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed

您尝试插入的数据似乎有问题(双id?至少有您的约束)

于 2012-04-13T07:16:22.247 回答