Hi I want to know when the Oncreate() method of SqliteOPenHelper will called when i am create Object of MyDatabase Manager class
Question is that when i run app it create database but table is not create and it throw exception table does not exit.
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);
}
*开发者_JS百科*@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) {
}
}
}
Here i called my 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();*/
}
}
At least part of the problem is a spelling error in your DATABASE_CREATE
string:
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);";
The word "exists" is misspelled as "exites". Corrected, it would be:
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);";
By the way, your code doesn't show whether your openDB
method is called anywhere. onCreate won't actually be called until you call getWritableDatabase, which is called through your openDB
method.
精彩评论