我已经关注Android Hive网站上的教程并根据我的需要调整它,因为我在Android应用程序中使用SQLite非常新。但是我似乎每次都遇到同样的错误。我在下面发布了我的logcat。你可以看到它说我的桌子'时代'没有列'乘以'但无论什么时候我觉得我已经解决了问题,通过重命名等等,它只会在我表格中的不同列上出现同样的问题。
我的类DatabaseHelper如下:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "TimeDatabase";
// Contacts table name
private static final String TABLE_TIMES = "times";
// Contacts Table Columns names
private static final String KEY_ID = "_id";
private static final String KEY_LENGTH = "length";
private static final String KEY_DIFFICULTY = "difficulty";
private static final String KEY_ADD = "addition";
private static final String KEY_SUBTRACT = "subtraction";
private static final String KEY_DIVISION = "division";
private static final String KEY_MULTIPLY = "multiply";
private static final String KEY_DECIMAL = "decimal";
private static final String KEY_NEGATIVE = "negative";
private static final String KEY_TIME = "time";
private static final String KEY_DATE = "date";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TIMES_TABLE = "CREATE TABLE " + TABLE_TIMES + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_LENGTH + " INTEGER, "
+ KEY_DIFFICULTY + " INTEGER, " + KEY_ADD + " BOOLEAN, " + KEY_SUBTRACT
+ " BOOLEAN, " + KEY_DIVISION + " BOOLEAN, " + KEY_MULTIPLY + " BOOLEAN, "
+ KEY_DECIMAL + " BOOLEAN, " + KEY_NEGATIVE + " BOOLEAN, " + KEY_TIME
+ " LONG, " + KEY_DATE + " DATETIME" + ")";
db.execSQL(CREATE_TIMES_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_TIMES);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
void addTimeData (TimeData timeData){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_LENGTH,timeData.getLength()); //Numbers of questions
values.put(KEY_DIFFICULTY,timeData.getDifficulty()); //Difficulty
values.put(KEY_ADD, timeData.getAdd()); //Add Questions?
values.put(KEY_SUBTRACT, timeData.getSubtract()); //Subtract Questions?
values.put(KEY_DIVISION,timeData.getDivision()); //Div Questions?
values.put(KEY_MULTIPLY,timeData.getMultiply()); //Mult Quetsions?
values.put(KEY_DECIMAL, timeData.getDecimals()); //Decimals?
values.put(KEY_NEGATIVE, timeData.getNegative()); //Negatives?
values.put(KEY_TIME, timeData.getTime()); //How long did it take?
values.put(KEY_DATE, dateFormat.format(date)); //todays date;
// Inserting Row
db.insert(TABLE_TIMES, null, values);
db.close(); // Closing database connection
}
//Getting all Data
public List<TimeData> getAllTimeData() throws ParseException {
List<TimeData> TimeList = new ArrayList<TimeData>();
//select all query
String selectQuery = "SELECT * FROM " + TABLE_TIMES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
TimeData tData = new TimeData();
tData.setID(Integer.parseInt(cursor.getString(0)));
tData.setLength(Integer.parseInt(cursor.getString(1)));
tData.setDifficulty(Integer.parseInt(cursor.getString(2)));
tData.setAdd(Boolean.parseBoolean(cursor.getString(3)));
tData.setSubtract(Boolean.parseBoolean(cursor.getString(4)));
tData.setDivision(Boolean.parseBoolean(cursor.getString(5)));
tData.setMultiply(Boolean.parseBoolean(cursor.getString(6)));
tData.setDecimals(Boolean.parseBoolean(cursor.getString(7)));
tData.setNegative(Boolean.parseBoolean(cursor.getString(8)));
tData.setTime(Long.parseLong(cursor.getString(9)));
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = dateFormat.parse(cursor.getString(10));
tData.setDate(dt);
// Adding contact to list
TimeList.add(tData);
} while (cursor.moveToNext());
}
return TimeList;
}
}
这是我的Logcat
06-16 19:57:55.213: E/SQLiteLog(20352): (1) table times has no column named multiply
06-16 19:57:55.245: E/SQLiteDatabase(20352): Error inserting time=20313415529 division=false length=10 multiply=false difficulty=11 negative=false date=2014-06-16 19:57:55 addition=true decimal=false subtraction=false
06-16 19:57:55.245: E/SQLiteDatabase(20352): android.database.sqlite.SQLiteException: table times has no column named multiply (code 1): , while compiling: INSERT INTO times(time,division,length,multiply,difficulty,negative,date,addition,decimal,subtraction) VALUES (?,?,?,?,?,?,?,?,?,?)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at com.sambrooks.mathlete.DatabaseHandler.addTimeData(DatabaseHandler.java:94)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at com.sambrooks.mathlete.AppActivity3.onClick(AppActivity3.java:252)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.view.View.performClick(View.java:4240)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.view.View$PerformClick.run(View.java:17721)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.os.Handler.handleCallback(Handler.java:730)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.os.Handler.dispatchMessage(Handler.java:92)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.os.Looper.loop(Looper.java:137)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at android.app.ActivityThread.main(ActivityThread.java:5103)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at java.lang.reflect.Method.invoke(Method.java:525)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-16 19:57:55.245: E/SQLiteDatabase(20352): at dalvik.system.NativeStart.main(Native Method)
我非常感谢任何帮助。我已经更改了数据库版本并卸载了应用程序并多次删除了它的数据,并且没有像其他答案所建议的那样修复问题。
答案 0 :(得分:1)
BOOLEAN
之后,你在这里错过了一个逗号。
... + KEY_DIVISION + " BOOLEAN " + KEY_MULTIPLY + " BOOLEAN, " ...
此execSQL()
调用是否会引发异常?
我猜这是相关的,因为问题在于KEY_MULTIPLY字段。