[英]Error creating a sqlite database giving syntax error
我正在使用android联系人列表应用程序,我刚刚开始在android平台上进行开发,在我的联系人列表应用程序中,我必须创建一个sqlite数据库表来存储联系人,但我在创建sqlite数据库时遇到了困难,这给我的logcat一个错误窗口,例如:“ Logcat显示这些错误” sqlite返回:错误代码= 1,msg =附近“,”语法错误“准备'CREATE TABLE Places(ID INT PRIMARY)时在0x1f14d0上出现故障1(在”,“附近:语法错误) KEY NOT NULL,名称TEXT NOT NULL,电话TEXT NOT NULL,地址TEXT NOT NULL,网站TEXT,HOME TEXT NOT NULL,'FATAL EXCEPTION:main“ java.lang.RuntimeException:无法启动活动ComponentInfo {com.webpreneur_contactlist / com .webpreneur_contactlist.WebpreneurActivity}:java.lang.IllegalStateException:数据库未打开”,请帮助我
public class DBHandler extends SQLiteOpenHelper{
private static final int DB_Version = 1;
private static final String DB_Name = "Places";
protected static final String Places_Table = "Places";
String name, Address, Website,ID;
//WebpreneurActivity Contact;
public static final String Key_ID = "ID";
public static final String Key_Name = "Name";
public static final String Key_Phone = "Phone";
public static final String Key_Address = "Address";
public static final String Key_Website = "Website";
public static final String Key_Home = "HOME";
public static final String PROJECTION[] = {
Key_ID,
Key_Name,
Key_Address,
Key_Phone,
Key_Home
};
String CREATE_PLACES_TABLE = "create table if not exists Places_Table (id integer primary key ,"+
"name VARCHAR not null, phone VARCHAR not null, address VARCAHR not null, website VARCHAR not null,Home VARCHAR not null)";
Context context;
SQLiteDatabase db;
public DBHandler(Context context) {
super(context, DB_Name, null, DB_Version);
Log.d("database1" ,"4");
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
Log.d("DB", "DB creationnewwwww");
db.execSQL("CREATE TABLE " + Places_Table + "(" +
Key_ID + " INT PRIMARY KEY NOT NULL," +
Key_Name + " TEXT NOT NULL," +
Key_Phone + " TEXT NOT NULL," +
Key_Address + " TEXT NOT NULL," +
Key_Website + " TEXT," +
Key_Home + " TEXT,");
Log.d("DB", "DB creationnewwwwwwwwwwwwwwwwwwwwwwwwwww");
}
catch(SQLiteException e){
Log.d("DB", "DB creation excptionhhhhhhhhhhhhhhh");
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("Drop Table If Exists" + Places_Table);
onCreate(db);
}
public DBHandler open()
{Log.d("DB", "DB creation 9");
db = this.getWritableDatabase();
onCreate(db);
Log.d("DB", "DB creation 9");
return this;
}
public void close()
{
db.close();
}
//Adding Places
void addPlaces( int id, String name,String phone, String address,String url){
Log.d("DB", "DB creation 1");
//SinglePlaceActivity single = new SinglePlaceActivity(); Log.d("DB", "DB creation 2");
ContentValues contentValues = new ContentValues();
Log.d("DB", "DB creation 3");
contentValues.put(Key_ID, id);
Log.d("DB", "DB creation 4");
contentValues.put(Key_Name, name);
contentValues.put(Key_Phone, phone);
contentValues.put(Key_Address, address);
contentValues.put(Key_Website, url);
Log.d("DB", "DB creation 4");
db.insert(Places_Table, null, contentValues);
Log.d("DB", "DB creation 5555");
//db.close();
}
public String getdata() {
// TODO Auto-generated method stub
String [] columns =new String[]{Key_ID ,Key_Name,Key_Address,Key_Website};
Cursor c =db.query(DB_Name, columns, null, null, null, null, null);
String Result="";
int iRow=c.getColumnIndex(Key_ID);
int iName=c.getColumnIndex(Key_Name);
int iAddress=c.getColumnIndex(Key_Address);
int iWebsite=c.getColumnIndex(Key_Website);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
Result=Result+c.getString(iRow)+" "+c.getString(iName)+ " "+c.getString(iAddress)+ " "+c.getString(iWebsite)+ "\n";
}
return Result;
}
public void createRow(String name, String address, String Phone, String home)
{
ContentValues initialValues = new ContentValues();
initialValues.put(Key_Name, name);
initialValues.put(Key_Address, address);
initialValues.put(Key_Phone, Phone);
initialValues.put(Key_Home, home);
//pass the initialValues to the database to insert the row
db.insert(Places_Table, null, initialValues);
}
public void deleteRow(long rowId){
db.delete(Places_Table, Key_ID+"="+rowId,null);
}
public boolean updateRow (long rowId, String name, String address, String Phone, String home){
ContentValues args = new ContentValues();
args.put(Key_Name, name);
args.put(Key_Address, address);
args.put(Key_Phone, Phone);
args.put(Key_Home, home);
return db.update(Places_Table, args, Key_ID +"="+ rowId, null)>0;
}
public Cursor fetchRow(long rowId) throws SQLException{
Cursor result = db.query( Places_Table, null,
Key_ID + "=" + rowId, null, null, null,null);
if ((result.equals(rowId)) || !result.isFirst()) {
throw new SQLException("No note matching ID: " + rowId);
}
return result;
}
public Cursor fetchAllRows(){
Log.d("Your Location4", "ok99:");
return db.query(Places_Table, PROJECTION,
null, null, null, null, null);
}
}
更换
Key_Home + " TEXT,");
与
Key_Home + " TEXT)");
有一个,
因此可以期待更多的列规范,但没有后续内容,因此您需要使用)
终止表规范。
另外,您不应该在数据库助手onCreate()
捕获SQLiteException
。 当onCreate()
正常返回时,假定数据库创建成功。
解决问题之后,请卸载您的应用程序,以便删除不正确的旧数据库文件,并再次运行onCreate()
。
同样在open()
您似乎自己调用了onCreate()
。 不要那样做 get{Read,Writ}ableDatabase()
会在需要时为您完成。
您没有关闭SQL语句。 例如:
这个:
db.execSQL("CREATE TABLE " + Places_Table + "(" +
Key_ID + " INT PRIMARY KEY NOT NULL," +
Key_Name + " TEXT NOT NULL," +
Key_Phone + " TEXT NOT NULL," +
Key_Address + " TEXT NOT NULL," +
Key_Website + " TEXT," +
Key_Home + " TEXT,");
应该:
db.execSQL("CREATE TABLE " + Places_Table + "(" +
Key_ID + " INT PRIMARY KEY NOT NULL," +
Key_Name + " TEXT NOT NULL," +
Key_Phone + " TEXT NOT NULL," +
Key_Address + " TEXT NOT NULL," +
Key_Website + " TEXT," +
Key_Home + " TEXT)");
然后将关闭create table语句。 由于逗号,它正在期待另一个参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.