我正在学习编码并制作了一个秒表,它将laptimes保存在String中并将日期作为String获取。我想把它们放在一个SQLite数据库中(所以我以后可以在listview中显示日期并在另一个显示所有laptimes的活动中打开它。)我已经跟踪了互联网上的一些代码并尝试放入什么东西在一起所以我可能会查看我的代码中的一些东西。我已经对我认为理解的部分发表评论,所以你可以稍微思考一下。
问题:当我按下保存时,执行以下代码并返回toastmessage:Somehting出错了。
[^ => Match any character that IS NOT...
A-Z0-9 => ...A-Z, 0-9 or a space (there's a space on the end of that).
] => End of character group.
AddData方法如下:
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String dateStamp = getCurrentTimeStamp();
AddData(dataInput, dateStamp);
//DatabaseHelper.deleteAll();
}
}); //save data though AddData method as input the listText
DatabaseHelper类中的布尔方法是:
public void AddData(String time, String date) {
boolean insertData = DatabaseHelper.addData(time, date);
if (insertData) {
toastMessage(dataInput);
} else {
toastMessage("Something went wrong");
}
}
当我在addData()中只输入1个变量但不在后面实现的2中输入时,它可以工作。我认为它应该有效。下面我还列出了用于创建SQLite数据库的代码。
public boolean addData(String times, String date) { //addData that takes a string
SQLiteDatabase db = this.getWritableDatabase(); //database called db and use getWritableDatabase method
ContentValues contentValues = new ContentValues(); //make a new object of ContentValues
contentValues.put(COL_2, times); //put COL_2 and the String in the ContentValues object
contentValues.put(COL_3, date);
long result = db.insert(TABLE_NAME, null, contentValues); //insert contentValues object into the table
//if date as inserted incorrectly it will return -1
if (result == -1) {
db.close();
return false;
} else {
return true;
}
}
我希望有人可以帮助我找到问题所以我可以了解更多。
答案 0 :(得分:1)
您的创建表格查询在COL_2 +" TEXT"
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " ( " + COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_2 +" TEXT, " + COL_3 +" TEXT)"; // Added a comma after the COL_2
db.execSQL(createTable);
}
答案 1 :(得分:0)
我可以快速浏览一个问题,这是创建语句中缺少的逗号,这意味着您的数据库未按预期创建。请尝试以下修正案。
String createTable = "CREATE TABLE " + TABLE_NAME + " ( " + COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_2 +" TEXT," + COL_3 +" TEXT)";