4

I am getting android.database.sqlite.SQLiteException: near "SELECT ": syntax error (code 1): , while compiling: SELECT * FROM universities Error.

I have following Classes

User Class as

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

And MainActivity as

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);
}
3
  • This will destroy the whole table creation: + "(" + COLUMN_UNIV_ID + " integer RIMARY KEY," + COLUMN_UNIV_NAME RIMARY should be PRIMARY Commented Aug 1, 2014 at 8:13
  • @FrankN.Stein SQLite just ignores unknown words. Commented Aug 1, 2014 at 8:14
  • @PramodYadav That has no relation with the error message. Commented Aug 1, 2014 at 8:14

2 Answers 2

3

Looks like the space after SELECT is not an ordinary space (0x20) but some other character, such as non-breaking space (0xa0). Replace it with regular space.

Sign up to request clarification or add additional context in comments.

Comments

2

In theory, SELECT should be perfectly fine. However, the error message has a space after the SELECT, so it's possible that you have entered a non-breaking space or some other control character.

Anyway, to avoid formatting problems like this, better use a helper function like query:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.