我目前正在调整Android应用程序,但是有一个我无法解决的相当简单的问题。
在“ onClick”之后,我想在我的数据库中检索特殊数据(在这种情况下,是一种加密货币的名称),但是该数据存在并且名称很好,问题出在请求中,但不是解决方法。
由以下原因引起:android.database.sqlite.SQLiteException:无此类列:名称(代码1 SQLITE_ERROR):,而在编译时:选择* FROM COINS WHERE cryptoName = name
这是我的sqliteDatabase.java的源代码
package com.example.cryptoapp;
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;
import java.util.ArrayList;
public class SqliteDatabase extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 5;
private static final String DATABASE_NAME = "cryptoBase";
private static final String TABLE_COINS = "COINS";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_CRYPTONAME = "cryptoName";
private static final String COLUMN_QUANTITY = "quantity";
private static final String COLUMN_TOTALVALUE = "totalValue";
public SqliteDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_COINS_TABLE = "CREATE TABLE " + TABLE_COINS + "(" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_CRYPTONAME + " TEXT," + COLUMN_QUANTITY + " TEXT," + COLUMN_TOTALVALUE + " TEXT" + ")";
db.execSQL(CREATE_COINS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COINS);
onCreate(db);
}
public ArrayList<Coins> listCoins(){
String sql = "select * from " + TABLE_COINS;
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<Coins> storeCoins = new ArrayList<>();
Cursor cursor = db.rawQuery(sql, null);
if(cursor.moveToFirst()){
do{
int id = Integer.parseInt(cursor.getString(0));
String cryptoname = cursor.getString(1);
String quantity = cursor.getString(2);
String totalvalue = cursor.getString(3);
storeCoins.add(new Coins(id, cryptoname, quantity, totalvalue));
}while (cursor.moveToNext());
}
cursor.close();
return storeCoins;
}
public void addCoins(Coins coins){
Log.d("TESTINADD", coins.getCryptoName());
ContentValues values = new ContentValues();
values.put(COLUMN_CRYPTONAME, coins.getCryptoName());
values.put(COLUMN_QUANTITY, coins.getQuantity());
values.put(COLUMN_TOTALVALUE, coins.getTotalValue());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_COINS, null, values);
}
public void updateCoins(Coins coins){
ContentValues values = new ContentValues();
values.put(COLUMN_CRYPTONAME, coins.getCryptoName());
values.put(COLUMN_QUANTITY, coins.getQuantity());
values.put(COLUMN_TOTALVALUE, coins.getTotalValue());
SQLiteDatabase db = this.getWritableDatabase();
db.update(TABLE_COINS, values, COLUMN_ID + " = ?", new String[] { String.valueOf(coins.getId())});
}
public Coins findCrypto(String name){
String query = "Select * FROM " + TABLE_COINS + " WHERE " + COLUMN_CRYPTONAME + " = " + "name";
SQLiteDatabase db = this.getWritableDatabase();
Coins coins = null;
Log.d("TESTquery", query);
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()){
int id = Integer.parseInt(cursor.getString(0));
String mName = cursor.getString(1);
String mQuantity = cursor.getString(2);
String mTotalValue = cursor.getString(3);
coins = new Coins(id, mName, mQuantity, mTotalValue);
}
cursor.close();
Log.d("TEST", name);
Log.d("TEST1", coins.getCryptoName());
return coins;
}
public void deleteCoins(int id){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_COINS, COLUMN_ID + " = ?", new String[] { String.valueOf(id)});
}
}
提前谢谢!
答案 0 :(得分:2)
String query = "Select * FROM " + TABLE_COINS + " WHERE " + COLUMN_CRYPTONAME + " = " + "name";
这里应该是:
String query = "Select * FROM " + TABLE_COINS + " WHERE " + COLUMN_CRYPTONAME + " LIKE " + "'" + name + "'";
答案 1 :(得分:1)
您将名称作为字符串而不是变量进行传递,因此它经过了硬编码,因此数据库中没有名为名称的表,因此您遇到了例外情况,可以解决此问题,使查询像这样
String query = "Select * FROM " + TABLE_COINS + " WHERE " + COLUMN_CRYPTONAME + " = " + name;