Android SQLite表不存在

时间:2015-06-14 17:21:15

标签: java android sqlite

我正在尝试在SQLite表中创建和插入行。但是,插入表不存在的行时出错。谁能告诉我这里做错了什么?谢谢!

调用类来创建数据库并插入行的活动中的代码如下:

    // Open database
    db = new DBAdapter(this);
    db.open();

    // TODO: insertRecept statements hieronder verwijderen
    // Dienen enkel voor test om inhoud te hebben
    long id = db.insertRecept("aardappelen - bloemkool - kotelet");
    id = db.insertRecept("rijst - wokgroenten - scampi - currysaus");
    id = db.insertRecept("nasi goreng - omelet");
    id = db.insertRecept("ebly - veggieburger - provencaalse saus");
    id = db.insertRecept("rijst - kipfilet - zoetzure saus");
    id = db.insertRecept("ebly - veggieburger - provencaalse saus");
    id = db.insertRecept("puree - fish-sticks - spinazie");
    id = db.insertRecept("tortellini - kaassaus");
    id = db.insertRecept("aardappelen - bloemkool - chippolata");
    id = db.insertRecept("pizza");
    id = db.insertRecept("frietjes");
    id = db.insertRecept("aardappel - zalm - prei - melksaus");
    db.close();

DBAdapter类如下:

package be.bertcarremans.weekmenu;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.sql.SQLException;

public class DBAdapter {
static final String KEY_ROWID = "_id";
static final String KEY_RECEPT_TITEL = "recept_titel";
static final String TAG = "DBAdapter";

static final String DATABASE_NAME = "recepten_db";
static final String DATABASE_TABLE = "recepten";
static final int DATABASE_VERSION = 1;

static final String DATABASE_CREATE =
        "CREATE TABLE recepten (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
        + "recept_titel  TEXT NOT NULL);";

DatabaseHelper DBHelper;
SQLiteDatabase db;

final Context context;

// Constructor
public DBAdapter(Context ctx) {
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
    }

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

// database openen
public DBAdapter open() {
    db = DBHelper.getWritableDatabase();
    return this;
}

// database sluiten
public void close() {
    DBHelper.close();
}

// recept toevoegen
public long insertRecept(String recept) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_RECEPT_TITEL, recept);
    return db.insert(DATABASE_TABLE, null, initialValues);
}
}

1 个答案:

答案 0 :(得分:2)

看起来您很可能已经更改了架构结构,并且(很可能)更新了数据库的版本号。这与损坏的onUpgrade()实施相结合导致了您的问题。

作为一种快速解决方案:卸载应用程序并再次安装 - 这将完全擦除数据库并使您的应用程序在第一次运行时创建它。但作为长期解决方案,您需要修复onUpgrade()。现在你这样做:

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

因此在人类语言中它会显示:"当我们需要升级时,只需擦除表格"。之后重新创建呢?如果你不这样做,你没有桌子。如果您没有计划拥有更新代码,那么我将表格删除/创建代码移动到(分别){ie} createTables() / removeTables()方法,然后让onUpgrade()使用@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { removeTables(db); createTables(db); } 它:

import xml.etree.ElementTree as et
import itertools

raw_xml = '''<foo>
<spam taste="great"> stuff</spam> <spam taste="moldy"> stuff</spam>
<bar taste="eww"> stuff </bar> <bar> stuff </bar> 
<bacon taste="yum"> stuff </bacon><bacon taste="yum"> stuff </bacon><bacon taste="yum"> stuff </bacon>
<spam taste="Great">stuff2</spam>
</foo>'''

groups = itertools.groupby(et.fromstring(raw_xml), lambda element: element.tag)
groups = [list(group[1]) for group in groups]

print groups