创建SQlite数据库时出错

时间:2013-07-13 08:17:46

标签: android android-sqlite

我正在创建一个包含三列的表。这是Dasebase.java类::

    package digicare.phonebook;

    import android.R;
    import android.app.Activity;
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View.OnCreateContextMenuListener;

    public class Database {


private static final String DATABSE_NAME ="MY_DB";
private static final String DATABSE_TABLE ="MY_TABLE";
private static final int DATABASE_VERSION=1;


public static final String KEY_ID = "ID" ;
public static final String  PHONE_NUM ="NUMBER";
public static final String PERSON_NAME="NAME";

public static final String DATABASE_CREATE =" CREATE TABLE " + DATABSE_TABLE + " (" + 
                                             KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                                             PHONE_NUM + " TEXT NOT NULL, "+ 
                                             PERSON_NAME + " TEXT NOT NULL)" ;


private DatabaseHelper myHelper;

private final Context mycontext;
private SQLiteDatabase myDB;


private static class DatabaseHelper extends SQLiteOpenHelper{

    public DatabaseHelper(Context context) {
        super(context, DATABSE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        db.execSQL(DATABASE_CREATE);

    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS "+ DATABSE_TABLE );
        onCreate(db);
    }

}

public Database(Context c){
    mycontext=c;
}
public Database open() throws SQLException {

    myHelper=new DatabaseHelper(mycontext);
    myDB= myHelper.getWritableDatabase();
    return this ;
    }
public void close(){
    myHelper.close();
}
public long createEntry(String number, String name) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues() ;
    cv.put(PERSON_NAME, name);
    cv.put(PHONE_NUM,number);
    return myDB.insert(DATABSE_TABLE, null, cv);
}
    }

从其他类我调用createEntry()方法在数据库中插入一行,但LogCat给出错误:“没有这样的表:MY_TABLE”

通过我调用createEntry方法的代码:

    btn_save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String number =edtxt_num.getText().toString();
            String name =edtxt_name.getText().toString();
            //number and name
            Boolean diditwork=true;
            try{
            Database entry = new Database(enter_data.this);
            entry.open();
            entry.createEntry(number , name);

            entry.close();
            }catch(Exception e ){
                Log.w("error", e.getMessage());
                diditwork=false;
            }finally{
                if (diditwork){
                    Dialog d = new Dialog(getApplicationContext());
                    d.setTitle("Ring Manager");
                    TextView tv = new TextView(getApplicationContext()) ;
                    tv.setText("Saved");
                    d.setContentView(tv);
                    d.show();

                }
            }


        }
    });

我可以先创建表吗????通过调用onCreate(db)方法????或者当我插入新行时自动创建????

1 个答案:

答案 0 :(得分:0)

只有在数据库尚不存在的情况下才会调用

DatabaseHelper.onCreate()。如果您进行了先前的测试并创建了没有该表的数据库,则以后不会创建它。

可能的解决方案:

  • 删除数据库(应位于目录“/ data / data /< app package name> / databases”)
  • 增量DATABASE_VERSION