Android中SQLITEDATABASE中的语法错误

时间:2012-10-10 13:31:47

标签: android database

在我的数据库中,我想从我的数据库中检索一行。为此,我写了一个查询:

Cursor mCursor = db.query("Author_table", column_name, "Author_name=?" , params, null , null , null) ;

通过这个我想做以下查询:“从Author_table中选择AID,其中Author_name = Sultan”。但我在android中的SQLiteDatabase中有一个异常。请建议我做什么??

3 个答案:

答案 0 :(得分:1)

这样的事情应该有效:

Cursor c = db.query("Author_table", new String[] {"AID"}, "Author_name=?", new String[] {"Sultan"}, null, null, null);

修改

SQLOpenHelper

import java.io.File;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;

public class OpenHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "test.db";
    private static final int DATABASE_VERSION = 8;

    private static final String CREATE_TABLE_USER = "create table USER( "
            + "username varchar(256) not null, " + "sex varchar(1) not null, "
            + "password varchar(64) not null, " + "active int not null"
            + ");";

    public OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_USER);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        if (oldVersion < 8) {
            // db update goes here and in onCreate() (for newly installed apps)
        }
    }

}

答案 1 :(得分:0)

用单引号包装字符串值。您的查询应该看起来像

Select AID from Author_table where Author_name = 'Sultan';

而不是

Select AID from Author_table where Author_name = Sultan;

答案 2 :(得分:0)

尝试此查询

Cursor cursor = db.query("Author_table", new String[] { "AID" },
                    "Author_name LIKE ?", new String[] { "%Sultan%" }, null, null,null);