0

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

问题是,当我运行应用程序时,它创建数据库但未创建表,并且它抛出异常表不退出。

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();*/


    }
}
4

1 回答 1

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);";

“exists”这个词被拼错为“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方法是否在任何地方被调用。在您调用getWritableDatabase之前,实际上不会调用onCreate,这是通过您的方法调用的。openDB

于 2011-07-09T05:08:03.410 回答