简体   繁体   中英

android.database.sqlite.SQLiteException: no such column: name (code 1 SQLITE_ERROR)

I'm currently tweaking an Android application and I have a fairly simple problem that I can't solve.

Following an "onClick" I want to retrieve a special data in my DB (in this case, the name of a crypto currency), but the data exists, and the names are good, the problem comes from the request but not way to solve it.

Caused by: android.database.sqlite.SQLiteException: no such column: name (code 1 SQLITE_ERROR): , while compiling: Select * FROM COINS WHERE cryptoName = name

Here is the source code of my 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)});
    }
}

Thank you in advance!

String query = "Select * FROM " + TABLE_COINS + " WHERE " + COLUMN_CRYPTONAME + " = " + "name";

这里应该是:

String query = "Select * FROM " + TABLE_COINS + " WHERE " + COLUMN_CRYPTONAME + " LIKE " + "'" + name + "'";

您将名称作为字符串而不是变量传递,因此它是硬编码的,因此您的数据库中没有名为 name 的表,因此您得到了这个异常来解决问题,使查询像这样

String query = "Select * FROM " + TABLE_COINS + " WHERE " + COLUMN_CRYPTONAME + " = " + name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM