SQLite数据库..onCreate()不会被调用 [英] SQLite database ..onCreate() is not being called

查看:138
本文介绍了SQLite数据库..onCreate()不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Kohli.java

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
    }


}

我写了下面code键使数据库的表...是:: qwert111111 qwert22222 constructor111111 qwert3333 ..这是OnCreate函数没有被调用,所以也根本就不创建的logcat输出数据库......

i wrote the following code to make a database with a table... the output at logcat is :: qwert111111 qwert22222 constructor111111 qwert3333 .. that is the oncreate function is not being called so the database is also not created...

推荐答案

数据库没有真正建立,直到你叫<一href="http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getWritableDatabase%28%29"><$c$c>getWritableDatabase()在   dbHelper 对象。

The database isn't actually created until you call getWritableDatabase() on the dbHelper object.

这篇关于SQLite数据库..onCreate()不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆