gpt4 book ai didi

java - Android Java SQLite 数据库 : The database does not take 2 String inputs

转载 作者:行者123 更新时间:2023-11-30 10:28:52 24 4
gpt4 key购买 nike

我正在学习编码并制作了一个秒表,它将单圈时间保存在一个字符串中,并将日期也作为一个字符串获取。我想把它们放在 SQLite 数据库中(这样我以后可以在 ListView 中显示日期并在另一个显示所有单圈时间的 Activity 中打开它。)我遵循了互联网上的一些代码并尝试将它们放在一起所以我可能会查看代码中的一些内容。我对我认为我理解的部分进行了评论,因此您可以稍微了解一下我的想法。

问题:当我按下保存时,会执行以下代码并返回提示消息:出错了。

 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

AddData方法如下:

public void AddData(String time, String date) {
boolean insertData = DatabaseHelper.addData(time, date);

if (insertData) {
toastMessage(dataInput);
} else {
toastMessage("Something went wrong");
}
}

DatabaseHelper 类中的 boolean 方法是这样的:

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;
}

}

当我在 addData() 中仅输入 1 个变量但不输入我后来实现的 2 个变量时,它会起作用。我认为它应该工作。下面我还放了用于制作 SQLite 数据库的代码。

public static final String TABLE_NAME = "stopwatch";   //make a table with name
public static final String COL_1 = "ID"; //make an ID for every colomn
public static final String COL_2 = "times"; //make a 2nd colomn for data
public static final String COL_3= "date";

public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) { //make the onCreate method that takes the database as input
String createTable = "CREATE TABLE " + TABLE_NAME + " ( " + COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_2 +" TEXT" + COL_3 +" TEXT)"; //create the table with SQL statements to input the data correctly
db.execSQL(createTable); //input the SQL statements in the DB

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //make upgrade method that takes the database and the versions
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME); //execute SQL statements drop table and which one
onCreate(db); //run through create method
}

我希望有人能帮助我找到问题,以便我了解更多信息。

最佳答案

您的创建表查询在 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);

}

关于java - Android Java SQLite 数据库 : The database does not take 2 String inputs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44357726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com