【问题标题】:SQLite database ..onCreate() is not being calledSQLite 数据库 ..onCreate() 没有被调用
【发布时间】:2012-02-14 18:22:21
【问题描述】:

Kohli.java

package com.kohli;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.content.Context;

public class KohlifActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

          Log.i("KOHLIActivity", "qwert11111111");   

          setContentView(R.layout.main);
          Log.i("KOHILActivity", "qwert22222222222"); 

        DbHelper1 DbHelper=new DbHelper1(this) ;

        Log.i("KOHLIfActivity", "qwert3333333333");



    }
}



DbHelper1.java
package com.kohli;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

public class DbHelper1 extends SQLiteOpenHelper 
{

    static final String TAG = "DbHelper1";
    static final String DB_NAME = "timeline.db"; 
    static final int DB_VERSION = 1;
    static final String TABLE = "timeline"; 
    static final String C_ID = BaseColumns._ID;
    static final String C_CREATED_AT = "created_at";
    static final String C_SOURCE = "source";
    static final String C_TEXT = "txt";
    static final String C_USER = "user";
    Context context;

    public DbHelper1(Context context) { 
        super(context, DB_NAME, null, DB_VERSION);
        this.context = context;
        Log.d(TAG, "constructor111111");
        //System.out.println("dbhelper constructor");
    }

    // Called only once, first time the DB is created

    public void onCreate(SQLiteDatabase db) {
    String sql = "create table " + TABLE + " (" + C_ID + " int primary key, "
    + C_CREATED_AT + " int, " + C_USER + " text, " + C_TEXT + " text)"; 

    db.execSQL(sql); 
    Log.d(TAG, "onCreated sql:22222222 ");
    //System.out.println("dbhelper oncreate");
    }

    // Called whenever newVersion != oldVersion
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // Typically do ALTER TABLE statements, but...we're just in development,
    // so:
    db.execSQL("drop table if exists " + TABLE); // drops the old database
    Log.d(TAG, "onUpdated 33333333");
    //System.out.println("dbhelper onupgrade");
    onCreate(db); // run onCreate to get new database
    }


}

我编写了以下代码来创建一个带有表的数据库... logcat 的输出是 :: qwert111111 qwert22222 constructor111111 qwert3333 .. 即未调用 oncreate 函数,因此也未创建数据库...

【问题讨论】:

    标签: java android sqlite


    【解决方案1】:

    在您调用 getWritableDatabase() dbHelper 对象。

    【讨论】:

    • 非常感谢您的回复...:)
    • 但即使调用getWritableDatabase() 也不会调用onCreate(SQLiteDatabase a)。请问有什么建议吗?
    • @Daniel onCreate 方法将在您调用getWritableDatabase/getReadableDatabase 时调用,仅当数据库在模拟器/设备上尚不存在时(因此可以创建它)。如果您对此有任何疑问,您应该发布一个提供更多详细信息的问题。
    • 确保增加版本。如果版本不增加其数量,则不会重新创建数据库。
    猜你喜欢
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    相关资源
    最近更新 更多