简体   繁体   中英

trying to call user_id from sqlite

I'm trying to call user_id from sqlite database but I get this error

android.database.sqlite.SQLiteException: unknown error (code 0 SQLITE_OK): Queries can be performed using SQLiteDatabase query or rawQuery methods only. at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)

public Database user_id(){
    SQLiteDatabase db = getReadableDatabase();
    String query = String.format("SELECT user_id FROM OrderDetails ;");
    db.execSQL(query);
    Cursor c = db.rawQuery(query, null);
    if (c != null && c.moveToFirst()) {

        c.moveToFirst();
    }
        return user_id();
}

Please don't recommend another solution because I'm stuck with this for more than a day and I tried almost all solutions represented in stackoverflow and online

From: https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase#execSQL(java.lang.String)

public void execSQL (String sql)
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.

So remove this line:

db.execSQL(query);

The method execSQL() is not used to execute queries that fetch rows, like SELECT statements. You can use it with INSERT, UPDATE, CREATE, etc.
Also, I think that you want to return the user's ids as a Cursor , right?
So why:

public Database user_id()

this returns a Database object (whatever this is).
Change the method to this:

public Cursor user_id(){
    SQLiteDatabase db = getReadableDatabase();
    String query = "SELECT user_id FROM OrderDetails";
    Cursor c = db.rawQuery(query, null);
    return c;
}

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