SQLite的语法错误在创建表的方法 [英] SQLite syntax error in create table method
本文介绍了SQLite的语法错误在创建表的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
logcat的说thare是单词表,在这里数据库类近一个SQLite错误。我看着这个code和我没有看到任何ayntax arrors,我失去了一些东西?
数据库类
公共类数据库{
公共静态最后弦乐DATABASE =测试;
公共静态最终诠释DATABASE_VERSION = 1;
公共静态最后弦乐TABLENAME =表;
公共静态最后弦乐_ID =_id;
// collumns
公共静态最后弦乐样品=样本;
私有静态最后弦乐SCRIPT_CREATE_TABLE =
CREATE TABLE IF NOT EXISTS+ TABLENAME +(+ _ID +INTEGER PRIMARY KEY AUTOINCREMENT,+
样品+文本);
SQLiteDatabase sqLiteDatabase;
SQLiteHelper sqLiteHelper;
上下文语境;
公共数据库(上下文C){
上下文= C;
}
公共无效openToRead()抛出android.database.SQLException {
sqLiteHelper =新SQLiteHelper(上下文,数据库,空,DATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
}
公共无效openToWrite()抛出android.database.SQLException {
sqLiteHelper =新SQLiteHelper(上下文,数据库,空,DATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
}
公共无效的close(){
sqLiteHelper.close();
}
公共无效deleteDB(){
context.deleteDatabase(数据库);
}
公共无效插入(){
ContentValues contentValues =新ContentValues();
contentValues.put(SAMPLE,TEST 1);
sqLiteDatabase.insert(TABLENAME,空,contentValues);
}
公共字符串的get(){
字符串returnString =;
光标光标= sqLiteDatabase.query(TABLENAME,新的String [] {}样本,NULL,NULL,NULL,NULL,NULL);
如果(光标!= NULL){
cursor.moveToFirst();
returnString = cursor.getString(cursor.getColumnIndex(SAMPLE));
}
返回returnString;
}
公共类SQLiteHelper扩展SQLiteOpenHelper {
公共SQLiteHelper(上下文的背景下,字符串名称,
CursorFactory工厂,INT版){
超(背景下,名称,厂家,版本);
}
//的onCreate的SQLiteOpenhelper只称为如果数据库不存在
@覆盖
公共无效的onCreate(SQLiteDatabase DB){
db.execSQL(SCRIPT_CREATE_TABLE);
}
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
db.execSQL(DROP TABLE IF EXISTS+ SCRIPT_CREATE_TABLE);
的onCreate(DB);
}
}
}
MainAcivity类
公共类MainActivity延伸活动{
TextView的textViewOne;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
textViewOne =(TextView中)findViewById(R.id.textView1);
数据库DB =新的数据库(本);
db.openToWrite();
db.insert();
db.close();
db.openToRead();
字符串getStr = db.get方法就会();
db.close();
textViewOne.setText(getStr);
}
}
解决方案
您已经有了表名表 - 这就是问题所在,因为它的的SQLite的关键字。
如果你想创建表的名称应该引用它下面这样的:
CREATE TABLE IF NOT EXISTS表......
logcat says thare is a SQLite error near the word "table" in the database class here. I looked over this code and I don't see any ayntax arrors , am i missing something?
Database class
public class Database {
public static final String DATABASE = "tester";
public static final int DATABASE_VERSION = 1;
public static final String TABLENAME = "table";
public static final String _ID = "_id";
// collumns
public static final String SAMPLE = "sample";
private static final String SCRIPT_CREATE_TABLE =
"CREATE TABLE IF NOT EXISTS " + TABLENAME + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
SAMPLE + " TEXT)";
SQLiteDatabase sqLiteDatabase;
SQLiteHelper sqLiteHelper;
Context context;
public Database(Context c){
context = c;
}
public void openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, DATABASE, null, DATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
}
public void openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, DATABASE, null, DATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
}
public void close(){
sqLiteHelper.close();
}
public void deleteDB(){
context.deleteDatabase(DATABASE);
}
public void insert(){
ContentValues contentValues = new ContentValues();
contentValues.put(SAMPLE, "TEST ONE");
sqLiteDatabase.insert(TABLENAME, null, contentValues);
}
public String get(){
String returnString = "";
Cursor cursor = sqLiteDatabase.query(TABLENAME, new String[]{SAMPLE}, null, null, null, null, null);
if(cursor!=null){
cursor.moveToFirst();
returnString = cursor.getString(cursor.getColumnIndex(SAMPLE));
}
return returnString;
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
// onCreate of the SQLiteOpenhelper only called if the database does not already exist
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SCRIPT_CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + SCRIPT_CREATE_TABLE);
onCreate(db);
}
}
}
MainAcivity class
public class MainActivity extends Activity {
TextView textViewOne;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewOne = (TextView) findViewById(R.id.textView1);
Database db = new Database(this);
db.openToWrite();
db.insert();
db.close();
db.openToRead();
String getStr = db.get();
db.close();
textViewOne.setText(getStr);
}
}
解决方案
You've got table with name "table" - this is the problem, because it's SQLite's keyword.
If you would like to create table with that name you should quote it like below:
CREATE TABLE IF NOT EXISTS "table" ...
这篇关于SQLite的语法错误在创建表的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文