SqliteOpenHelper在调用OnCreate()方法时

时间:2011-07-09 03:55:30

标签: android sqliteopenhelper

您好我想知道当我创建MyDatabase Manager类的对象时,SqliteOPenHelper的Oncreate()方法何时会被调用

问题是,当我运行app时,它会创建数据库,但是表没有创建,并且抛出异常表不会退出。

package pk.com.db;

import org.apache.http.Header;

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

public class AdatabaseManager {

    // Constant through out the class we use

    private SQLiteDatabase sqlitedatabase; //reference to the database manaager 
    private final String DB_NAME = "MYBooks";
    private final int DB_VERSION= 1;


    //Now table name and it's fields

    private final String DB_TABLE="Book_Details"; //this all constant are use as Key
    private final String FK_ID="ID";
    private final String Field_ONE="NAME";
    private final String Field_TWO="PRICE";
    private final String Field_THREE="AUTHER";
    private CustomSqliteOpenHelper helper;

     private static final String DATABASE_CREATE =
            "create table if not exites Book_Details(_id integer primary key autoincrement, "
            + "NAME text not null,PRICE text not null, "
            + "AUTHER text not null);";



    public AdatabaseManager(Context context){
        helper=new CustomSqliteOpenHelper(context);
        this.sqlitedatabase=helper.getReadableDatabase();


    }
    public void openDb(){
        try
        {
            this.sqlitedatabase=helper.getWritableDatabase();



        }catch (Exception e) {

            Log.i(getClass().getName(), e.toString());
            System.out.print(e);
        }

    }
    public void closedb(){
        try{
            helper.close();

        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void addRow(String strName,String strPrice,String strAuother){
        try{

            ContentValues values=new ContentValues();
            values.put(Field_ONE,strName);
            values.put(Field_TWO,strPrice);
            values.put(Field_THREE,strAuother); 

            sqlitedatabase.insert(DB_TABLE,null,values);

        }catch (Exception e) {
            Log.i(getClass().getName(),e.toString());

        }       
    }

    private class CustomSqliteOpenHelper extends SQLiteOpenHelper
{
    public CustomSqliteOpenHelper(Context context){

        super(context,DB_NAME,null,DB_VERSION);

    }


    **@Override
    public void onCreate(SQLiteDatabase db) {
    try{



            db.execSQL(DATABASE_CREATE);



    }catch (Exception e) {
        System.out.println("********************************");
        System.out.print(e);
        System.out.println("********************************");
    }

    }**

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}

}

这里我调用了我的AdatabaseManager

//************************************
import android.app.Activity;
import android.os.Bundle;

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

        AdatabaseManager _adm=new AdatabaseManager(this);  //it think it open database 
       /* _adm.openDb();
        _adm.addRow("ANDROID EBOOKS","2999","KINGRAJARANN");
        _adm.closedb();*/


    }
}

1 个答案:

答案 0 :(得分:0)

至少部分问题是DATABASE_CREATE字符串中的拼写错误:

 private static final String DATABASE_CREATE =
        "create table if not exites Book_Details(_id integer primary key autoincrement, "
        + "NAME text not null,PRICE text not null, "
        + "AUTHER text not null);";

“存在”这个词拼写错误为“exites”。更正了,它将是:

 private static final String DATABASE_CREATE =
        "create table if not exists Book_Details(_id integer primary key autoincrement, "
        + "NAME text not null,PRICE text not null, "
        + "AUTHER text not null);";

顺便说一句,您的代码不会显示您的openDB方法是否在任何地方被调用。在您拨打通过openDB方法调用的onCreate之前,系统才会实际调用getWritableDatabase