android.database.sqlite.SQLiteException:near“SELECT”:语法错误(代码1):,同时编译:SELECT * FROM Table_Name

时间:2014-08-01 08:10:46

标签: android sqlite

我得到android.database.sqlite.SQLiteException:接近“SELECT”:语法错误(代码1):,编译时:SELECT * FROM Universities错误。

我有以下课程

用户类为

public class User {
    private int univ_id;

    private String univ_name, univ_abbr;

    public User(int univ_id, String univ_name, String univ_abbr) {

        this.univ_id = univ_id;
        this.univ_name = univ_name;
        this.univ_abbr = univ_abbr;
    }

    public User(String univ_name, String univ_abbr) {

        this.univ_name = univ_name;
        this.univ_abbr = univ_abbr;
    }

    public User() {
        // TODO Auto-generated constructor stub
    }

    public int getUniv_id() {
        return univ_id;
    }

    public void setUniv_id(int univ_id) {
        this.univ_id = univ_id;
    }

    public String getUniv_name() {
        return univ_name;
    }

    public void setUniv_name(String univ_name) {
        this.univ_name = univ_name;
    }

    public String getUniv_abbr() {
        return univ_abbr;
    }

    public void setUniv_abbr(String univ_abbr) {
        this.univ_abbr = univ_abbr;
    }
}

DBStorageFirst Class as

public class DBStorageFirst extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "STUPIDSID_OFFLINE_DB";

    public static final String TABLE_NAME_UNIVERSITIES = "universities";


    public static final String COLUMN_UNIV_ID = "univ_id";
    public static final String COLUMN_UNIV_NAME = "univ_name";
    public static final String COLUMN_UNIV_ABBR = "univ_abbr";

    public DBStorageFirst(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("create table " + TABLE_NAME_UNIVERSITIES
                + "(" + COLUMN_UNIV_ID + " integer RIMARY KEY," + COLUMN_UNIV_NAME
                + " text," + COLUMN_UNIV_ABBR + " text )");
        Log.e("Success", "Universities Table Created");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_UNIVERSITIES);

        onCreate(db);
    }

    public void addUniversities(int univ_id, String univ_name, String univ_abbr) {
         SQLiteDatabase db = this.getWritableDatabase();
        String sql1 = "insert into " + DBStorageFirst.TABLE_NAME_UNIVERSITIES
                + " (" + DBStorageFirst.COLUMN_UNIV_ID + ","
                + DBStorageFirst.COLUMN_UNIV_NAME + ","
                + DBStorageFirst.COLUMN_UNIV_ABBR + ")" + " values(" + univ_id
                + ",'" + univ_name + " ','" + univ_abbr + "')";
        db.execSQL(sql1);
        // database.execSQL(sql2);
        Log.e("Success", "Data inserted Successfully into Universities Table");
    }

    public List<User> getAllContacts() {
        List<User> contactList = new ArrayList<User>();

        String selectQuery = "SELECT * FROM "+TABLE_NAME_UNIVERSITIES;
        SQLiteDatabase db1 = this.getWritableDatabase();
        Cursor cursor = db1.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
                User contact = new User();
                contact.setUniv_id(Integer.parseInt(cursor.getString(0)));
                contact.setUniv_name(cursor.getString(1));
                contact.setUniv_abbr(cursor.getString(2));
                contactList.add(contact);
            } while (cursor.moveToNext());
        }
        return contactList;
    }
}

和MainActivity一样

DBStorageFirst db = new DBStorageFirst(this);

// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addUniversities(25, "ABC", "UA");
db.addUniversities(26, "DEF", "UB");
List<User> list = new ArrayList<User>();
list = db.getAllContacts();

for (User cn : list) {
    String log = "Id: " + cn.getUniv_id() + " ,Name: "
            + cn.getUniv_name() + " ,Abbr: " + cn.getUniv_abbr();
    // Writing Contacts to log
    Log.d("Name: ", log);
}

2 个答案:

答案 0 :(得分:3)

看起来SELECT之后的空格不是普通空格(0x20),而是一些其他字符,例如非空格(0xa0)。用常规空间替换它。

答案 1 :(得分:2)

理论上,SELECT应该完全没问题。 但是,错误消息在SELECT之后有一个空格,因此您可能已输入不间断空格或其他控制字符。

无论如何,为了避免格式化这样的问题,最好使用像query这样的辅助函数:

Cursor cursor = db1.query(TABLE_NAME_UNIVERSITIES, null,
                          null, null, null, null, null);