【问题标题】:android.database.sqlite.SQLiteException: near "SELECT ": syntax error (code 1): , while compiling: SELECT * FROM Table_Nameandroid.database.sqlite.SQLiteException: near "SELECT ": syntax error (code 1): , while compile: SELECT * FROM Table_Name
【发布时间】:2014-08-01 08:10:46
【问题描述】:

我得到 android.database.sqlite.SQLiteException: near "SELECT ": syntax error (code 1): , while compile: SELECT * FROM university Error.

我有以下课程

用户类为

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 类为

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);
}

【问题讨论】:

  • 这将破坏整个表的创建:+ "(" + COLUMN_UNIV_ID + " integer RIMARY KEY," + COLUMN_UNIV_NAME RIMARY 应该是 PRIMARY
  • @FrankN.Stein SQLite 只是忽略未知单词。
  • @PramodYadav 这与错误信息无关。

标签: android sqlite


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2015-04-24
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      相关资源
      最近更新 更多