我很难弄清楚为什么在向表中插入值时会出现错误。 谢谢你的帮助。
基本上,该代码仅在只有两列和一个contentValues.put函数时才起作用,但是,当我添加另一列时,尝试插入值时总是出错,我也不知道为什么。
我已经检查了SQlite查询等。
package com.avrahamzilberblat.battleshipfinal;
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;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
private static final String TABLE_NAME = "people_table";
private static final String COL1 = "ID";
private static final String COL2 = "name";
private static final String COL3 = "scoreRatio";
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +COL2 +" TEXT,"+ COL3 +"INTEGER"+");";
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT,"+COL3+"TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item,int scoreRatio) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
contentValues.put(COL3, 5);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
/**
* Returns all the data from database
* @return
*/
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Returns only the ID that matches the name passed in
* @param name
* @return
*/
public Cursor getItemID(String name,Double scoreRatio){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL1 + " FROM " + TABLE_NAME +
" WHERE " + COL2 + " = '" + name + "'" + " AND " +COL3 + " = '" + scoreRatio+"'";
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Updates the name field
* @param newName
* @param id
* @param oldName
*/
public void updateName(String newName, int id, String oldName){
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_NAME + " SET " + COL2 +
" = '" + newName + "' WHERE " + COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + oldName + "'";
Log.d(TAG, "updateName: query: " + query);
Log.d(TAG, "updateName: Setting name to " + newName);
db.execSQL(query);
}
/**
* Delete from database
* @param id
* @param name
*/
public void deleteName(int id, String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + TABLE_NAME + " WHERE "
+ COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + name + "'";
Log.d(TAG, "deleteName: query: " + query);
Log.d(TAG, "deleteName: Deleting " + name + " from database.");
db.execSQL(query);
}
public void clearDatabase(String TABLE_NAME) {
SQLiteDatabase db = this.getWritableDatabase();
String clearDBQuery = "DELETE FROM "+TABLE_NAME;
db.execSQL(clearDBQuery);
}
}
--------------------这是我想要一个字符串和双精度数据类型的更新,仍然无法正常工作
@Override
public void onCreate(SQLiteDatabase db) {
//String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +COL2 +" TEXT,"+ COL3 +"INTEGER"+");";
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT,"+COL3 +"REAL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item,int scoreRatio) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, "ood");
contentValues.put(COL3, 6.6);
答案 0 :(得分:1)
您应该更改:-
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT,"+COL3+"TEXT)");
至:-
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY, " +
COL2 +" TEXT,"+COL3+" TEXT)");
然后您应该执行以下一项操作:-
增加数据库版本,例如将super(context, TABLE_NAME, null, 1);
更改为super(context, TABLE_NAME, null, 2);
,然后重新运行该应用程序。
上面的内容很重要,因为 onCreate 在创建数据库时只会自动运行一次,这可能是问题的根本原因。您已添加一列,但实际上尚未添加。
我玩过很多次,我实际上希望它是一个实数 例如Double,所以一开始我写的是REAL,而不是TEXT
SQlite允许将任何类型的数据存储在任何类型的列中,只有一个例外,那就是rowid的别名(使用INTEGER PRIMARY KEY定义的列(带或不带AUTOINCREMENT))。 rowid的别名只能存储一个整数(带符号的64位),并且在表中必须是UNIQUE。
其中包括:-
AUTOINCREMENT关键字强加了额外的CPU,内存,磁盘空间和 磁盘I / O开销,如果没有严格要求,则应避免。它是 通常不需要。