我正在尝试创建一个包含两个EditTexts的小程序,供用户输入他的名字和密码,以及一个按钮,使用Android中的SQLite将它们添加到表中, 但我通过我的SQLException得到了那条消息:
android.database.sqlite.SQLiteException:near“table”:语法错误(code1):,编译时:DROP TABLE IF EXISTS
在我的logcat中出现这一行:(1)在“table”附近:语法错误
我无法弄清问题在哪里
这是我的代码:
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);
}
}
}
}
并在 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");
}
}
}
答案 0 :(得分:1)
您无法创建(也不执行任何其他操作)名为... table
的表,因为table
是SQLite中的保留字(通常在SQL中)。
由于未创建表,因此无法删除。
尝试调用它myTable
(或者可能是一个更重要的名称),例如元素名称(通常是复数形式:ie:Persons,Contacts,Animals,Orders,...)
答案 1 :(得分:0)
尝试将表放在单引号中并在语句末尾使用分号,如下所示:
private static final String TABEL_NAME = "table";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS '" + TABEL_NAME + "';";