如何在Android中为我的应用程序创建本地数据库

时间:2014-10-20 08:58:53

标签: java android sqlite

我想在sqlite中为我的android应用程序创建一个简单的数据库。我只想将我的值添加到表中。

这是我的代码,但这对我不起作用,因为该表未在DDMS上显示。

public class QuickSubmitContext extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "submitManager";
    private static final String TABLE_CONTACTS = "submit";
    private static final String KEY_ID = "id";
    private static final String KEY_PH_NO = "phone_number";
    private static final String KEY_DNCA = "donotca";
    private static final String KEY_CA = "callagain";
    private static final String KEY_NOTE = "note";
    public QuickSubmitContext(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_PH_NO + " TEXT,"
                + KEY_DNCA + " TEXT," + KEY_CA + " TEXT," + KEY_NOTE + " TEXT" + ")";
            db.execSQL(CREATE_CONTACTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

        // Create tables again
        onCreate(db);
    }
}

logcat的

10-21 04:39:00.230: E/AndroidRuntime(835): FATAL EXCEPTION: main
10-21 04:39:00.230: E/AndroidRuntime(835): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aspeage.quikw/com.aspeage.quikw.FragmentLayout}: java.lang.NullPointerException
10-21 04:39:00.230: E/AndroidRuntime(835):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-21 04:39:00.230: E/AndroidRuntime(835):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-21 04:39:00.230: E/AndroidRuntime(835):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-21 04:39:00.230: E/AndroidRuntime(835):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-21 04:39:00.230: E/AndroidRuntime(835):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 04:39:00.230: E/AndroidRuntime(835):  at android.os.Looper.loop(Looper.java:137)
10-21 04:39:00.230: E/AndroidRuntime(835):  at android.app.ActivityThread.main(ActivityThread.java:5041)
10-21 04:39:00.230: E/AndroidRuntime(835):  at java.lang.reflect.Method.invokeNative(Native Method)
10-21 04:39:00.230: E/AndroidRuntime(835):  at java.lang.reflect.Method.invoke(Method.java:511)
10-21 04:39:00.230: E/AndroidRuntime(835):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-21 04:39:00.230: E/AndroidRuntime(835):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-21 04:39:00.230: E/AndroidRuntime(835):  at dalvik.system.NativeStart.main(Native Method)
10-21 04:39:00.230: E/AndroidRuntime(835): Caused by: java.lang.NullPointerException
10-21 04:39:00.230: E/AndroidRuntime(835):  at com.aspeage.quikw.FragmentLayout.onCreate(FragmentLayout.java:79)
10-21 04:39:00.230: E/AndroidRuntime(835):  at android.app.Activity.performCreate(Activity.java:5104)
10-21 04:39:00.230: E/AndroidRuntime(835):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-21 04:39:00.230: E/AndroidRuntime(835):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-21 04:39:00.230: E/AndroidRuntime(835):  ... 11 more
10-21 04:39:07.090: I/Process(835): Sending signal. PID: 835 SIG: 9
10-21 04:39:13.050: E/Trace(866): error opening trace file: No such file or directory (2)

1 个答案:

答案 0 :(得分:4)

创建新SQLite数据库的推荐方法是创建SQLiteOpenHelper的子类并覆盖onCreate()方法,您可以在其中执行SQLite命令以在数据库中创建表。例如:

public class DictionaryOpenHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 2;
    private static final String DICTIONARY_TABLE_NAME = "dictionary";
    private static final String DICTIONARY_TABLE_CREATE =
                "CREATE TABLE " + DICTIONARY_TABLE_NAME + " (" +
                KEY_WORD + " TEXT, " +
                KEY_DEFINITION + " TEXT);";

    DictionaryOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

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

以下是说明。 http://developer.android.com/guide/topics/data/data-storage.html#db