0

数据库处理程序代码:

public class DatabaseHandler extends SQLiteOpenHelper { 

private static final String DATABASE_NAME = "mydb";

// 1st table --------------------------------------
private static final String TABLE_MODULE = "module";    
// Module table columns
private static final String KEY_MODULE_CODE = "code";
private static final String KEY_MODULE_TITLE = "title";
private static final String KEY_MODULE_SEMESTER = "semester";
private static final String KEY_MODULE_TIMETABLE= "timetable";


// 2nd table --------------------------------------
private static final String TABLE_URIS = "applicationURLs"; 
// Module table columns
private static final String KEY_SERVER_URI = "serverURI";
private static final String KEY_NEWS_URI = "newsURI";
private static final String KEY_MODULES_URI= "moduleURI";

// Constructor
public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

/**
 * Creating tables
 */
@Override
public void onCreate(SQLiteDatabase db) {

      String CREATE_MODULE_TABLE = "CREATE TABLE "+ TABLE_MODULE + "("
            +   KEY_MODULE_CODE         +" TEXT PRIMARY KEY," 
            +   KEY_MODULE_TITLE        +" TEXT," 
            +   KEY_MODULE_TIMETABLE    +" TEXT," 
            +   KEY_MODULE_SEMESTER     +" TEXT);";     

      String CREATE_URIS_TABLE = "CREATE TABLE " + TABLE_URIS + "("
            + KEY_SERVER_URI    + " TEXT PRIMARY KEY," 
            + KEY_NEWS_URI      + " TEXT," 
            + KEY_MODULES_URI   + " TEXT);";

      db.execSQL(CREATE_MODULE_TABLE + CREATE_URIS_TABLE);       
}

/**
 * Upgrading database
 */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_MODULE);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_URIS);
    //Create tables again
    onCreate(db);
}

public void addModule(Module module) {

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(KEY_MODULE_TITLE, module.getTitle());                
    values.put(KEY_MODULE_CODE, module.getCode());  
    values.put(KEY_MODULE_TIMETABLE, module.getTimetable()); 
    values.put(KEY_MODULE_SEMESTER, module.getSemester());  

    db.insert(TABLE_MODULE, null, values); 
    db.close(); // Closing database connection
}

/**
 * Adding new server URIs
 * @param uris
 */
public void addURIs(ServerURIs uris){

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(KEY_SERVER_URI, uris.getServerURI()); 
    values.put(KEY_NEWS_URI, uris.getNewsURI());        
    values.put(KEY_MODULES_URI, uris.getModulesURI());  

    // Inserting Row
    db.insert(TABLE_URIS, null, values);
    db.close(); // Closing database connection
}
}

将新记录添加到第二个表时会引发错误,指出第二个表不存在。onCreate() 中将数据添加到数据库的活动中的代码:

DatabaseHandler dbh  = new DatabaseHandler(this);
dbh.addURIs(serverURLs);

不知道是什么原因。先感谢您。

4

1 回答 1

4

您不能在一次调用中执行多个语句,只需使用两个。

db.execSQL(CREATE_MODULE_TABLE);
db.execSQL(CREATE_URIS_TABLE);       

引用 SQLiteDatabase 的execSQL(String sql)文档

参数
sql - 要执行的 SQL 语句。不支持以分号分隔的多个语句。

于 2013-04-09T17:14:56.970 回答