多个SQLite表

时间:2012-07-03 14:29:31

标签: android sqlite multiple-tables android-sqlite

我已经尝试了将近一周的时间来创建一个包含多个表的SQLite数据库,但没有成功。 我已经搜索了几个小时,看了这个主题的所有主题,我不知道我做错了什么。

这是我从互联网上获得的代码,用于一张桌子,我刚刚添加了另一张代码,它不再适用了。

适配器:

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

public class WorkingAdapter {

    public static final String WORKINGDATABASE_NAME = "WORKING_DATABASE";
    public static final String WORKINGDATABASE_TABLE = "WORKING_TABLE";
    public static final int MYDATABASE_VERSION = 1;
    public static final String KEY_ID = "_id";
    public static final String KEY_CONTENT = "Content";

    //create table MY_DATABASE (ID integer primary key, Content text not null);
    private static final String SCRIPT_CREATE_WORKING_DATABASE =
        "create table " + WORKINGDATABASE_TABLE + " ("
        + KEY_ID + " integer primary key autoincrement, "
        + KEY_CONTENT + " text not null);";

    public static final String WORKINGDATABASE_TABLE2 = "MY_TABLE2";
    public static final String KEY_ID2 = "_id2";
    public static final String KEY_CONTENT2 = "Content2";

    //create table MY_DATABASE (ID integer primary key, Content text not null);
    private static final String SCRIPT_CREATE_WORKING_DATABASE2 =
        "create table " + WORKINGDATABASE_TABLE2 + " ("
        + KEY_ID2 + " integer primary key autoincrement, "
        + KEY_CONTENT2 + " text not null);";

    private SQLiteHelper sqLiteHelper;
    private SQLiteDatabase sqLiteDatabase;

    private Context context;

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

    public WorkingAdapter openToRead() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, WORKINGDATABASE_NAME, null, MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getReadableDatabase();
        return this;    
    }

    public WorkingAdapter openToWrite() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, WORKINGDATABASE_NAME, null, MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        return this;    
    }

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

    public long insert(String content){

        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_CONTENT, content);
        return sqLiteDatabase.insert(WORKINGDATABASE_TABLE, null, contentValues);
    }

public long insert2(String content){

        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_CONTENT2, content);
        return sqLiteDatabase.insert(WORKINGDATABASE_TABLE2, null, contentValues);
    }

    public int deleteAll(){
        return sqLiteDatabase.delete(WORKINGDATABASE_TABLE, null, null);
    }

    public Cursor queueAll(){
        String[] columns = new String[]{KEY_ID, KEY_CONTENT};
        Cursor cursor = sqLiteDatabase.query(WORKINGDATABASE_TABLE, columns, 
                null, null, null, null, null);

        return cursor;
    }

    public Cursor queueAll2(){
        String[] columns = new String[]{KEY_ID2, KEY_CONTENT2};
        Cursor cursor = sqLiteDatabase.query(WORKINGDATABASE_TABLE2, columns, 
                null, null, null, null, null);

        return cursor;
    }

    public class SQLiteHelper extends SQLiteOpenHelper {

        public SQLiteHelper(Context context, String name,
                CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(SCRIPT_CREATE_WORKING_DATABASE);
            db.execSQL(SCRIPT_CREATE_WORKING_DATABASE2);

        }

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

    }

}

的活动:

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class AndroidSQLite extends Activity {

    private WorkingAdapter myWorkingAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView listContent = (ListView)findViewById(R.id.contentlist);

        /*
         *  Create/Open a SQLite database
         *  and fill with dummy content
         *  and close it
         */
        myWorkingAdapter = new WorkingAdapter(this);
        myWorkingAdapter.openToWrite();
        myWorkingAdapter.deleteAll();

        myWorkingAdapter.insert("A for Apply");
        myWorkingAdapter.insert("B for Boy");
        myWorkingAdapter.insert("C for Cat");
        myWorkingAdapter.insert("D for Dog");
        myWorkingAdapter.insert("E for Egg");
        myWorkingAdapter.insert("F for Fish");
        myWorkingAdapter.insert("G for Girl");
        myWorkingAdapter.insert("H for Hand");
        myWorkingAdapter.insert("I for Ice-scream");
        myWorkingAdapter.insert("J for Jet");
        myWorkingAdapter.insert("K for Kite");
        myWorkingAdapter.insert("L for Lamp");
        myWorkingAdapter.insert("M for Man");
        myWorkingAdapter.insert("N for Nose");
        myWorkingAdapter.insert("O for Orange");
        myWorkingAdapter.insert("P for Pen");
        myWorkingAdapter.insert("Q for Queen");
        myWorkingAdapter.insert("R for Rain");
        myWorkingAdapter.insert("S for Sugar");
        myWorkingAdapter.insert("T for Tree");
        myWorkingAdapter.insert("U for Umbrella");
        myWorkingAdapter.insert("V for Van");
        myWorkingAdapter.insert("W for Water");
        myWorkingAdapter.insert("X for X'mas");
        myWorkingAdapter.insert("Y for Yellow");
        myWorkingAdapter.insert("Z for Zoo");

        myWorkingAdapter.insert2("W FOR WORKING");

        myWorkingAdapter.close();

        /*
         *  Open the same SQLite database
         *  and read all it's content.
         */
        myWorkingAdapter = new WorkingAdapter(this);
        myWorkingAdapter.openToRead();

        Cursor cursor = myWorkingAdapter.queueAll2();
        startManagingCursor(cursor);

        String[] from = new String[]{WorkingAdapter.KEY_CONTENT};
        int[] to = new int[]{R.id.text};

        SimpleCursorAdapter cursorAdapter =
            new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);

        listContent.setAdapter(cursorAdapter);

        myWorkingAdapter.close();


    }
}

我无休止地寻找带有多个表的SQLite数据库的exaples,并且没有工作,并且完全按照我在该主题上找到的所有页面中读到的,你能告诉我我做错了什么?

谢谢:)

4 个答案:

答案 0 :(得分:1)

变化

public static final int MYDATABASE_VERSION = 1;

public static final int MYDATABASE_VERSION = 2;

每次更改数据库更改时 递增它将触发onUpgrade 递减它将触发onDowngrade(如果你需要这样做,你当然会覆盖它)

您可以从android developer resources

了解有关构造函数的更多信息

答案 1 :(得分:0)

_idSQLLite表中的特殊列。它是每个表的主键,如果我没记错的话,如果你没有定义它,它将自动创建。我怀疑你把它改成_id2以某种方式导致问题。所以改回来:

public static final String KEY_ID2 = "_id";

尝试那样。

另请注意(现在),您的应用会在每次运行时尝试创建表格。因此,如果它在第一次运行时创建第一个表但无法创建第二个表并且您修复错误并再次运行您的应用程序,这次它将在创建第一个表时失败,因为它已经创建。因此,为了避免这些问题,请确保您进行清洁安装。正确处理数据库版本控制。

答案 2 :(得分:0)

要尝试的事情:

  1. SimpleCursorAdapter需要_id中名为Cursor的列。如果Cursor不包含名为_id的列,则会抛出异常。

    _id2更改回_id以解决此问题。

  2. 转到设置 - > Apps - > [您的应用] - >每次运行(更新的)应用程序时,清除数据。这很重要,因为每次运行应用程序时,您都希望100%确定您正在使用干净的平板。

  3. 使用ListActivity代替Activity。这使您的生活更轻松(不必直接处理ListView)并可能减少出错的可能性。请务必在setListAdapter(adapter)中致电onCreate

  4. 不要使用startManagingCursor ...它已被弃用为Android 3.0。您应该使用Android 3.0中引入的CursorLoaderLoaderManager(并在兼容包中支持Android 1.6)。

  5. 不要创建多个WorkingAdapter实例(或其他)。这只会让您的代码更加混乱,并且更有可能因忘记关闭它而泄漏SQLiteDatabase。你可以在这里阅读更多相关信息:

    How to make my SQLiteDatabase a Singleton?

答案 3 :(得分:0)

Caner说,你也不需要KEY_CONTENT2作为'Content2',列名在表中是唯一的,所以命名为'Content'就足够了。

另外,您是否尝试过清除应用数据库?转到设置 - >应用程序 - >您的应用程序 - >清除数据。您的数据库可能是在版本1中使用旧版本创建的,但您没有指定版本2,因此不会调用onUpgrade()方法。因此,要么清除应用程序数据库,要么将db版本提升为2。