jwandscheer
jwandscheer

Reputation: 178

Android SQL database Error while creating

I've made an E- Mail client for my Android Phone, and now I want to save the E- Mails in a SQLite database. But if I want to fill the Database only with test data, I this Error:

E/SQLiteLog(21212): (1) near "TO": syntax error

E/SQLiteDatabase(21212): Error inserting SUBJECT=test subject TO=emailadress2 FROM=emailadress1 CONTENT=test content

android.database.sqlite.SQLiteException: near "TO": syntax error (code 1): , while compiling: INSERT INTO MAIL(SUBJECT,TO,FROM,CONTENT) VALUES (?,?,?,?)

(...)

(1) near "FROM": syntax error

E/Error(21212): android.database.sqlite.SQLiteException: near "FROM": syntax error (code 1): , while compiling: SELECT ID, FROM, TO, SUBJECT, CONTENT FROM MAIL WHERE ID = -1

I use the following code for the database:

MAINACTIVITY.java

public void dbCreate(View view){
    String from = "emailadress1";
    String to = "emailadress2";
    String subject = "test subject";
    String content = "test content";

    try {
        datasource.open();
        datasource.createEntry(from, to, subject, content);
        datasource.close();
        }
        catch (Exception ex) {
            Toast.makeText(this,"Error", Toast.LENGTH_LONG).show();
            Log.e("Error", ex.toString());
            }
}

MYSQLiteHelper.java

private static final String DATABASE_NAME = "mail.db";
private static final int DATABASE_VERSION = 1;


private static final String TABLE_CREATE_MAIL = "CREATE TABLE MAILS (  ID integer PRIMARY KEY AUTOINCREMENT, FROM TEXT, TO TEXT, SUBJECT TEXT, CONTENT TEXT);";



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

}

@Override
public void onCreate(SQLiteDatabase database) {
    Log.i("Info", "OnCreate aufgerufen");
    database.execSQL(TABLE_CREATE_MAIL);

}

DATASOURCE.java

private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { "ID", "FROM",
        "TO", "SUBJECT", "CONTENT"};
public Entry createEntry(String from, String to, String subject, String content) {
    ContentValues values = new ContentValues();
    values.put("FROM", from);
    values.put("TO", to);
    values.put("SUBJECT", subject);
    values.put("CONTENT", content);

    long insertId = database.insert("MAIL", null,
            values);


    Cursor cursor = database.query("MAIL",allColumns, "ID = " + insertId, null, null, null, null, null);
    cursor.moveToFirst();

    return cursorToEntry(cursor);
}

Upvotes: 1

Views: 515

Answers (3)

Francisco Spaeth
Francisco Spaeth

Reputation: 23903

TO and FROM are reserved word for SQLLite

Please try:

INSERT INTO MAIL(SUBJECT,`TO`,`FROM`,CONTENT) VALUES (?,?,?,?)

If you don't want to execute an arbitrary SQL command, you could just rename the columns to TO_ and FROM_.

You could try to quote them like:

...
private String[] allColumns = { "ID", "`FROM`", "`TO`", "SUBJECT", "CONTENT"};
...
cv.put("`FROM`", from);
cv.put("`TO`", to);
...

Additionally: as commented by vinoth, your table name must be corrected (MAIL or MAILS)

Upvotes: 2

wael
wael

Reputation: 454

there is not an autoIncrement when creating your DataBAse.. and try this code when inserting data in the dataBase

 public long creatEntry(Srting id, String from,
    String to,String subject, String content) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues(); //ContentValues kima el bundle
cv.put("ID", id);
cv.put("FROM", from);
    cv.put("TO", to);
    cv.put("SUBJECT", subject);
    cv.put("CONTENT", content);

return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

Upvotes: 0

AnujMathur_07
AnujMathur_07

Reputation: 2596

FROM and TO are keywords in SQL, so you should change your column name.

The SQL standard specifies a huge number of keywords which may not be used as the names of tables, indices, columns, databases, user-defined functions, collations, virtual table modules, or any other named object

See this

Upvotes: 0

Related Questions