0

我正在尝试在 sqlite 中创建表,但显示无法创建表的错误。我有相同的代码,但数据库和表名不同。字段是相同的。还有一个疑问。我不能在包含相同字段的同一数据库中创建不同的表。

DbAdapter.java

代码如下。

public class DbAdapter {

    private static final String DATABASE_NAME="bible1";
    private static final String DATABASE_TABLE="test1";
    private static final int DATABASE_VERSION=1;

    public static final String KEY_ID="_id";
    public static final String UNITS="units";
    public static final String CHAPTERS="chapters";

    private static final String CREATE_DATABASE="create table test1 (_id integer primary key autoincrement, units text not null, chapters text not null);";

    private SQLiteHelper sqLiteHelper;
    private static SQLiteDatabase sqLiteDatabase;

    private Context context;

    //constructor

    public DbAdapter(Context c){
        context = c;
    }

    private static class SQLiteHelper extends SQLiteOpenHelper{

        public SQLiteHelper(Context context){

            super(context,DATABASE_NAME,null,DATABASE_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(CREATE_DATABASE);
            MakeUnits();
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

        }

    }

    public DbAdapter open() throws SQLException{
        try{
        sqLiteHelper = new SQLiteHelper(context);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        }catch(NullPointerException ne){
            Log.e("error in creating the table", "bye");
        }

        return this;
    }

    public void close(){
        sqLiteHelper.close();
    }

    public static long MakeUnits()
    {
        ContentValues insert1 = new ContentValues();
        insert1.put(UNITS, "Unit1");
        insert1.put(CHAPTERS, "CHAPTER1");
        return sqLiteDatabase.insert(DATABASE_TABLE,null,insert1);
    }

    public Cursor fetchAllNotes(){
        return sqLiteDatabase.query(DATABASE_TABLE, new String[]{KEY_ID,UNITS,CHAPTERS}, null, null, null, null, null);
    }
}

MainActivity.java

public class MainActivity extends ListActivity {

    private DbAdapter Database;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listContent = (ListView) findViewById(android.R.id.list);

        Database= new DbAdapter(this);
        Database.open();

        Cursor c = Database.fetchAllNotes();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

调用 open() 方法时出错。想不通。在 logcat 中,标签中的错误是“创建表时出错”

4

2 回答 2

1

您正在尝试调用sqLiteDatabase.insertinside MakeUnits,但sqLiteDatabase在您调用之前,此变量为空getWritableDatabase。您应该传递dbMakeUnits并调用 insert on db,而不是 on sqLiteDatabase

于 2012-10-25T06:22:25.443 回答
0

仅当您调用 getWritableDatabase()/getReadableDatabase() 时才会创建数据库

如果您没有可写数据库的权限,您将获得一个可读数据库。

    public SQLiteDatabase getWritableDB() {
        try {
            return helper.getWritableDatabase();
        } catch (Exception e) {
            return getReadableDB();
        }
    }

    public SQLiteDatabase getReadableDB() {
        return helper.getReadableDatabase();
    }
于 2012-10-25T06:21:13.490 回答