2014-01-22 53 views
0

我在工作android聯繫人列表應用程序,我剛開始在android平臺上開發,在我的聯繫人列表應用程序中,我必須創建一個sqlite數據庫表來存儲聯繫人,一個sqlite數據庫,它給我在我的logcat窗口上的錯誤,如: 「Logcat顯示這些錯誤」sqlite返回:錯誤代碼= 1,msg = near「,」 語法錯誤「Failure 1(near」,「:syntax error )在準備'創建表位置(ID INT PRIMARY KEY NOT NULL,名稱TEXT NOT NULL,電話TEXT NOT NULL,地址TEXT NOT NULL,網站TEXT,HOME TEXT NOT NULL,'致命異常:主要「java.lang。 RuntimeException:無法啓動活動ComponentInfo {com.webpreneur_contactlist/com.webpreneur_contactlist.WebpreneurActivity}:java.lang.IllegalStateException:數據庫未打開「 請幫我創建sqlite數據庫給出語法錯誤時出錯

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); 

     } 
} 
+0

「java.lang.IllegalStateException:數據庫未打開」可能是一條線索。 – Nick

+0

您可能想要使用不是INT而是[INTEGER](http://www.sqlite.org/lang_createtable.html#rowid)。 –

回答

2

更換

Key_Home + " TEXT,"); 

Key_Home + " TEXT)"); 

有一個,,使另一列規範預期,但沒有什麼跟隨着,所以你需要終止與表規範)

此外,您不應該在數據庫幫助程序onCreate()中捕獲SQLiteException。當onCreate()正常返回時,數據庫創建被認爲是成功的。

解決問題後,卸載您的應用程序,以便刪除舊的,不正確的數據庫文件並重新運行onCreate()

同樣在你的open()你似乎自己打電話onCreate()。不要這樣做。 get{Read,Writ}ableDatabase()將在需要時爲您做。

+0

嗨Laalto它對我很有幫助,但locat給我另一個錯誤「java.lang.RuntimeException:無法啓動活動ComponentInfo {com.webpreneur_contactlist/com.webpreneur_contactlist.WebpreneurActivity}:java.lang.IllegalStateException:數據庫未打開,請幫助我,您的指導將受到高度讚賞。 – Shazar

+0

檢查堆棧跟蹤在引發異常時您在代碼中的位置。 – laalto

1

你沒有關閉你的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語句。由於逗號,它期待另一個參數。