我正在尝试将数据保存到数据库中,但似乎没有运行onCreate方法。可能这可能是另一个问题,但logcat
12-01 01:22:41.785 19724-19724/com.example.user.timetable_test E/SQLiteLog: (1) near "null": syntax error
12-01 01:22:41.795 19724-19724/com.example.user.timetable_test E/SQLiteDatabase: Error inserting _length=1 _name=Break
android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: INSERT INTO null(_length,_name) VALUES (?,?)
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(near "null": syntax error (code 1): , while compiling: INSERT INTO null(_length,_name) VALUES (?,?))
#################################################################
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1058)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1607)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1479)
at com.example.user.timetable_test.setup.TableDBHandler.addEntry(TableDBHandler.java:79)
at com.example.user.timetable_test.setup.SetTable$PlaceholderFragment$1.onClick(SetTable.java:227)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10826)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
SQLite类是:
package com.example.user.timetable_test.setup;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.example.user.timetable_test.MiscData;
public class TableDBHandler extends SQLiteOpenHelper{
private MiscData data = MiscData.getInstance();
private static int DATABASE_VERSION = 1;
private static String DATABASE_NAME = "Timetable.db";
private static String[] TABLE_DAY = new String[MiscData.getInstance().getDays()];
private static String COLUMN_SESSION_NUM = "_id";
private static String COLUMN_NAME = "_name";
private static String COLUMN_LENGTH = "_length";
private SQLiteDatabase db;
public TableDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory,
int version){
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
db = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase){
Log.i("SQL", "DB Created");
for(int i = 0; i < data.getDays(); i++){
TABLE_DAY[i] = data.getDay(i + data.getFirstDay());
}
for(int i = 0; i < data.getDays(); i++){
String query = "CREATE TABLE " + TABLE_DAY[i] + "(" + COLUMN_SESSION_NUM +
" INTEGER PRIMARY KEY ," + COLUMN_NAME + " TEXT," +
COLUMN_LENGTH + " INTEGER " + ");";
sqLiteDatabase.execSQL(query);
}
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1){
Log.i("SQL", "DB updated");
for(int j = 0; j < data.getDays(); j++){
TABLE_DAY[j] = data.getDay(j + data.getFirstDay());
sqLiteDatabase.execSQL("DROP TABLE EXISTS " + TABLE_DAY[j]);
}
onCreate(sqLiteDatabase);
}
public void addEntry(int day, String name, int length){
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_LENGTH, length);
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_DAY[day], null, values);
db.close();
}
}
我使用数组来创建动态数量的表。
onCreate方法中的日志消息没有显示在logcat中,我检查了很多。
调用addEntry方法:
tableDBHandler.addEntry(page, sessionSpinners[i].getSelectedItem().toString(), Integer.parseInt(lengthText[i].getText().toString()));
如果我将DATABASE_VERSION
从1更改为onUpgrade
方法,但如果版本为1,则无法在logcat中看到来自onCreat
的消息。
这个类不应该创建.db文件吗?因为在单击按钮并运行该方法后,我在应用程序的文件夹中看不到任何文件。
答案 0 :(得分:4)
INSERT INTO null
- 表名为null。它来自您在SQLite助手TABLE_DAY
中初始化的onCreate()
数组。但是onCreate()
is only invoked when the database file is created,而不是每次打开数据库。
您可以将TABLE_DAY
初始值从onCreate()
移至例如构造
我使用数组来创建动态数量的表。
如果TABLE_DAY
数组真的是动态的,那你会遇到更多麻烦。考虑重新设计数据库模式,以便表是静态的,但内容可以是动态的。