2 servant s servant_s 于 2016.04.13 22:43 提问

关于sqlite一个数据库创建多张表的问题 求大神指教!

我是新手 最近自己尝试做个小程序。关于一个数据库建多张表 小弟查阅一下,也不能得到解决,求高手指教下。
public void onCreate(SQLiteDatabase db) {
String sqlCalendar = "CREATE TABLE IF NOT EXISTS calendar(pid integer primary key autoincrement," +
"moon varchar(4)," +
"day varchar(4)," +
"year varchar(4)," +
"hints varchar(8)," +
"sort varchar(4)," +
"tag text," +
"memorialDay varchar(4))";
db.execSQL(sqlCalendar);
String sqlSpend = "CREATE TABLE IF NOT EXISTS spend(spendid integer primary key autoincrement," +
"moon varchar(4)," +
"day varchar(4)," +
"year varchar(4)," +
"spendsort varchar(8)," +
"spend varchar(4))";
db.execSQL(sqlSpend);
}

public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS calendar");
    db.execSQL("DROP TABLE IF EXISTS spend");
    onCreate(db);
}
以上是帮助类的代码部分,服务部分的代码

public boolean insert(ScheduleDateTag sdt) {
    boolean flag = false;
    ContentValues values = new ContentValues();
    values.put("moon", sdt.getMoon());
    values.put("day", sdt.getDay());
    values.put("year", sdt.getYear());
    values.put("hints", sdt.getHints());
    values.put("sort", sdt.getSort());
    values.put("memorialDay", sdt.isMemorialDay());
    values.put("tag", sdt.getWorkingTag());
    try {
        db.insert("calendar", null, values);
        flag = true;
    } catch (Exception e) {
        flag = false;
    }
    return flag;
}
public boolean insert(SpendDateTag sdt) {
    boolean flag = false;
    db = dbOpenHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("moon", sdt.getMoon());
    values.put("day", sdt.getDay());
    values.put("year", sdt.getYear());
    values.put("spendsort", sdt.getSpendSort());
    values.put("spend", sdt.getSpend());
    try {
        db.insert("spend", null, values);
        flag = true;
    } catch (Exception e) {
        flag = false;
    }
    return flag;
}
这里创建calendar表可以创建,但是spend表创建不了,错误提示
android.database.sqlite.SQLiteException: no such table: spend (code 1): , while compiling: INSERT INTO spend(year,moon,day,spendsort,spend) VALUES (?,?,?,?,?)

求大神指教

1个回答

oyljerry
oyljerry   Ds   Rxr 2016.04.13 22:51
已采纳

你先create table创建表格 再insert插入数据

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!