无法在SQLite中创建多个表

时间:2015-01-10 07:58:33

标签: android sqlite datatable

我需要在SQLite数据库的3个表中为我的第一个Android应用创建和存储数据。我已经实现了一个表格,用户输入他的学术细节并通过给它一个标题来保存它。在ListActivity列表中显示的标题r中,通过单击列表项,将显示ViewActivity,其中显示该标题下的所有详细信息。这工作正常,但当我再创建1个表时,应用程序崩溃了。

DatabaseHelper类代码如下所示。

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String DB_NAME = "Mydata";
public static final String TABLE_NAME1 = "t1";
public static final String TITLE = "title";
public static final String NOTE = "note";

public DatabaseHelper(Context context, String name, CursorFactory factory,
        int version) {
    super(context, DB_NAME, factory, version);
}
@Override   
public void onCreate(SQLiteDatabase db) {
    final String createQuery1 = "CREATE TABLE " + TABLE_NAME1 + " (_id integer primary key autoincrement," + TITLE + ", " + NOTE + ");";
    db.execSQL(createQuery1);
    }

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Database will be wipe on version change
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME1);
    onCreate(db);
}

}

DatabaseConnector类代码如下。

public class DatabaseConnector {
 private static final String DB_NAME = "Mydata";
 private static final String TABLE_NAME1 = "t1";
 private static final String TITLE = "title";
 private static final String ID = "_id";
 private static final String NOTE = "note";
 private static final int DATABASE_VERSION = 1;
 private SQLiteDatabase database;
 private DatabaseHelper dbOpenHelper;

public DatabaseConnector(Context context) {
    dbOpenHelper = new DatabaseHelper(context, DB_NAME, null,
            DATABASE_VERSION);
}

public void open() throws SQLException {
    database = dbOpenHelper.getWritableDatabase();
}

public void close() {
    if (database != null)
        database.close();
}

public void InsertNote(String title, String note) {
    ContentValues newCon = new ContentValues();
    newCon.put(TITLE, title);
    newCon.put(NOTE, note);

    open();
    database.insert(TABLE_NAME1, null, newCon);
    close();
}

public void UpdateNote(long id, String title, String note) {
    ContentValues editCon = new ContentValues();
    editCon.put(TITLE, title);
    editCon.put(NOTE, note);

    open();
    database.update(TABLE_NAME1, editCon, ID + "=" + id, null);
    close();
}

public void DeleteNote(long id) {
    open();
    database.delete(TABLE_NAME1, ID + "=" + id, null);
    close();
}

public Cursor ListAllNotes() {
    return database.query(TABLE_NAME1, new String[] { ID, TITLE }, null,
            null, null, null, TITLE);
}

public Cursor GetOneNote(long id) {
    return database.query(TABLE_NAME1, null, ID + "=" + id, null, null,
            null, null);
}
}

还有3个其他类可以添加数据,显示标题列表和显示详细信息。 我在DatabaseHelper创建了另一个表,并创建了相应的DatabaseConnector和其他3个用于添加&的类。在第二个表格中显示详细信息。

添加新表后的DatabaseHelper新代码为

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

@Override   
public void onCreate(SQLiteDatabase db) {
    final String createQuery1 = "CREATE TABLE " + TABLE_NAME1 + " (_id integer primary key autoincrement," + TITLE + ", "   + NOTE + ");";

    final String createQuery2 = "CREATE TABLE " + TABLE_NAME2 + " (_id integer primary key autoincrement," + TITLE1 + ", "  + NOTE1 + ");";
    db.execSQL(createQuery1);
    db.execSQL(createQuery2);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME1 + TABLE_NAME2);
    onCreate(db);
}
}

我甚至不知道问题出在哪里,无论是创建表还是使用新的DatabaseConnector类。任何帮助将非常感谢!

0 个答案:

没有答案