SQLite 和 Android 异常
我在 Android 应用程序中遇到问题,我想使用以下脚本创建数据库。
CREATE TABLE IF NOT EXISTS events (
_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
source_device_id INT,
timestamp INT NOT NULL,
processed BOOLEAN DEFAULT FALSE,
event_type TINYINT NOT NULL,
payload BLOB);
CREATE INDEX IF NOT EXISTS events_unprocessed ON events (processed, _id);
我的 android 代码
public void onCreate(SQLiteDatabase db) {
InputStream schemaStream = context.getResources().openRawResource(R.raw.logdb_schema);
Scanner schemaScanner = new Scanner(schemaStream, "UTF-8");
schemaScanner.useDelimiter(";");
Log.i(TAG, "Creating database");
try {
db.beginTransaction();
while (schemaScanner.hasNext()) {
String statement = schemaScanner.next();
Log.d(TAG, "Creating database: " + statement);
db.execSQL(statement);
}
db.setTransactionSuccessful();
}
finally {
db.endTransaction();
}
}
无法正常工作,生成运行时异常:
android.database.sqlite.SQLiteException:不是错误:
I am facing problem in android application i want to create database with following script.
CREATE TABLE IF NOT EXISTS events (
_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
source_device_id INT,
timestamp INT NOT NULL,
processed BOOLEAN DEFAULT FALSE,
event_type TINYINT NOT NULL,
payload BLOB);
CREATE INDEX IF NOT EXISTS events_unprocessed ON events (processed, _id);
and my android code is
public void onCreate(SQLiteDatabase db) {
InputStream schemaStream = context.getResources().openRawResource(R.raw.logdb_schema);
Scanner schemaScanner = new Scanner(schemaStream, "UTF-8");
schemaScanner.useDelimiter(";");
Log.i(TAG, "Creating database");
try {
db.beginTransaction();
while (schemaScanner.hasNext()) {
String statement = schemaScanner.next();
Log.d(TAG, "Creating database: " + statement);
db.execSQL(statement);
}
db.setTransactionSuccessful();
}
finally {
db.endTransaction();
}
}
Its not working its generating run-time exception:
android.database.sqlite.SQLiteException: not an error:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到这个问题是因为我的架构中最后一个分号后面有空格。我的 java 代码将该空格作为单独的查询执行。
在 Froyo 上它会抱怨,在 Ice Cream Sandwich 上它不会造成问题。
I had this problem because of whitespace after the last semicolon in my schema. My java code was executing that whitespace as a separate query.
On Froyo it complains, on Ice Cream Sandwich it doesn't cause a problem.
我猜你正在使用 BOOLEAN &不支持 TINYINT 类型。您需要使用 INTEGER 代替
My guess would that you are using a BOOLEAN & TINYINT type which are not supported. You need to use INTEGER instead