طارق الجافي طارق الجافي - 1 month ago 22
Java Question

Error while compliling DROP IF EXISTS , syntax error near table Android SQLite

I am trying to make a small program that contains two EditTexts for user to enter his name and his password and a button which adds them in a table using SQLite in Android,
but I got that message by my SQLException :


android.database.sqlite.SQLiteException:near "table":syntax error (code1):,while compiling: DROP TABLE IF EXISTS


and in my logcat this line appear : (1) near "table" : syntax error

and I can't figure out where the problem

Here is my code :

package com.example.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseAdapter {

Helper helper ;
public DataBaseAdapter(Context context){
helper = new Helper ( context);
}
public long insertData (String name , String password)
{
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contenValues = new ContentValues ();
contenValues.put(Helper.NAME, name);
contenValues.put(Helper.PASSWORD, password);
long id = db.insert(Helper.TABEL_NAME, null, contenValues);
return id ;

}

static class Helper extends SQLiteOpenHelper {
private static final String DATABSE_NAME = "myDataBase";
private static final String TABEL_NAME = "table";
private static final int DATABASE_VERSION = 10;
public static final String NAME = "Name";
public static final String PASSWORD = "password";

private static final String CREATE_TABLE = "CREATE TABLE "+TABEL_NAME+" (_id INTEGER PRIMARY KEY AUTOINCREMENT , "+NAME+" TEXT not null , "+PASSWORD+" TEXT not null)";
private Context context;
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABEL_NAME;


public Helper(Context context) {

super(context, DATABSE_NAME, null, DATABASE_VERSION);

// TODO Auto-generated constructor stub
this.context = context;
Message.message(context, "constructor called");

}

@Override
public void onCreate(SQLiteDatabase db) {

// CREATE DATABSE

try {
db.execSQL(CREATE_TABLE);
Message.message(context, "OnCreate called");
} catch (SQLException e) {
// TODO Auto-generated catch block
Message.message(context, "" + e);
}
}

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

try {
db.execSQL(DROP_TABLE);
Message.message(context, "onUpgrade called");

onCreate(db);
} catch (SQLException e) {
// TODO Auto-generated catch block
Message.message(context, "" + e);
}

}
}

}


and in the MainActivity

public class MainActivity extends Activity {

EditText userName, password;

DataBaseAdapter helper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = new DataBaseAdapter(this);
userName = (EditText) findViewById(R.id.userText);
password = (EditText) findViewById(R.id.passText);

}

public void addUser(View view) {

String user = userName.getText().toString();
String pass = password.getText().toString();

long id = helper.insertData(user, pass);

if ( id <0 )
{
Message.message(this, "fail");
} else {
Message.message(this, "Suceeded");

}

}

}

Answer

You can't create (nor do any other operation) a table named... table, because table is a reserved word in SQLite (and in SQL in general).

Since the table is not created, it can't be deleted.

Try calling it myTable (or possibly a more significant name), such as the element name (normally, in its plural form: i.e.: Persons, Contacts, Animals, Orders, ...)