[英]Issue with android sql-query…“no such column”?
I want to use a SQL database to store records for a game. 我想使用SQL数据库存储游戏记录。 Actually I don't have a clue about SQL.
实际上,我对SQL没有任何了解。 I have a class "Records" which should manage the in and Output of records.
我有一个“记录”类,应该管理记录的输入和输出。 Additionally I have a class SQLDatabaseHelper which provides the SQL-Database.
另外,我有一个提供SQL数据库的SQLDatabaseHelper类。
My Problem is the following line: 我的问题是以下几行:
crsRecord = sqliteDatabase.rawQuery(QUERY_GET_RECORD + category, null);
I got always the error "No such column: SYS103" "SYS103" is a name of a category. 我总是收到错误“没有此类列:SYS103”“ SYS103”是类别的名称。 I don't know why it can be read.
我不知道为什么它可以被阅读。 Do you have any idea?
你有什么主意吗?
SQL table creation: SQL表创建:
CREATE TABLE records (
id INTEGER PRIMARY KEY AUTOINCREMENT,
category VARCHAR(30) NOT NULL,
displaytime VARCHAR(12) NOT NULL,
recordtime VARCHAR(10) NOT NULL);
I guess writing works just reading doesn't work. 我想写作品只是读书是行不通的。
public class Records {
private SQLiteOpenHelper sqliteOpenHelper;
private SQLiteDatabase sqliteDatabase;
private static final String INSERT_NEW_RECORD = "insert into records(category, displayrecord, timerecord) values(";
private static final String QUERY_GET_RECORD = "SELECT * FROM records WHERE category = ";
public Records(Context context){
sqliteOpenHelper = new SQLDatabaseHelper(context);
sqliteDatabase = sqliteOpenHelper.getWritableDatabase();
}
public void addRecord(String category, String displaytime, String timerecord){
ContentValues data = new ContentValues();
data.put("category", category);
data.put("displaytime", displaytime);
data.put("recordtime", timerecord);
sqliteDatabase.insert("records", null, data);
// sqliteDatabase.execSQL(INSERT_NEW_RECORD + category + ", " + strTime + ", " + dblTime + ");");
}
public String[] getRecord(String category){
String[] record = new String[3];
Cursor crsRecord;
try{
crsRecord = sqliteDatabase.rawQuery(QUERY_GET_RECORD + category, null);
}catch(SQLiteException e){
Log.d("database", e.getMessage());
String[] nullRecord = {category, "00:00.0", "0"};
return nullRecord;
}
int i=0;
while(crsRecord.moveToNext()){
record[i] = crsRecord.getString(0);
i++;
}
return record;
}
}
public class SQLDatabaseHelper extends SQLiteOpenHelper {
private Context context;
public SQLDatabaseHelper(Context context){
super(
context,
context.getResources().getString(R.string.dbname),
null,
Integer.parseInt(context.getResources().getString(R.string.version)));
this.context=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
for(String sql : context.getResources().getStringArray(R.array.create)){
db.execSQL(sql);
}
Log.d("Database", "creat succesfully");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
My method to get data out of the database, but for some reason the columnIndex is alway -1: 我从数据库中获取数据的方法,但是由于某种原因,columnIndex始终为-1:
public String[] getRecord(String category){
String[] record = new String[3];
Cursor crsRecord;
crsRecord = sqliteDatabase.rawQuery(QUERY_GET_RECORD, new String[]{ category } );
int i=0;
crsRecord.moveToFirst();
while(!crsRecord.isAfterLast()){
// Instead of using an int literal to get the colum index,
// use the getColumnIndex method
int index = crsRecord.getColumnIndex(category);
if (index == -1) {
String[] nullRecord = {category, "00:00.0", "0"};
return nullRecord;
}
else {
record[i] = crsRecord.getString(index);
i++;
}
crsRecord.moveToNext();
}
while(crsRecord.moveToNext()){
record[i] = crsRecord.getString(0);
i++;
}
return record;
}
You'll need to escape your parameters. 您需要转义参数。
As is, your code executes the query: 照原样,您的代码将执行查询:
SELECT * FROM records WHERE category = SYS103
That's not valid SQL. 那不是有效的SQL。 It should look like this:
它看起来应该像这样:
SELECT * FROM records WHERE category = 'SYS103'
and you need to escape apostrophes. 并且您需要转义撇号。 You'd be better off relying on
rawQuery
to escape your parameters: 您最好依靠
rawQuery
来转义参数:
private static final String QUERY_GET_RECORD
= "SELECT * FROM records WHERE category = ?";
and 和
crsRecord =
sqliteDatabase.rawQuery(QUERY_GET_RECORD, new String[]{ category } );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.