2011-03-02 52 views
0

我是最後一年的大學IT學生,我有一個問題,試圖在我的android項目中創建一個sqlite數據庫。 (我可以在編程但沒有專家,我已經設法通過各種教程找到一些代碼,但是當代碼運行時我收到了一條錯誤消息,它編譯的很好,其餘的應用程序工作,只是當它試圖把數據放到它會導致錯誤的數據庫Android SQLite異常表沒有列

數據庫適配器代碼:

package com.fyp; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class DBAdapter 
{ 
    public static final String KEY_ROWID = "_id"; 
    public static final String KEY_PFIRSTNAME = "pfirstname"; 
    public static final String KEY_PSURNAME = "psurname"; 
    public static final String KEY_PBUILDING= "pbuilding"; 
    public static final String KEY_PSTREET= "pstreet"; 
    public static final String KEY_PCITY = "pcity"; 
    public static final String KEY_PCOUNTY = "pcounty"; 
    public static final String KEY_PCOUNTRY = "pcountry"; 
    public static final String KEY_PPOSTCODE= "ppostcode"; 
    public static final String KEY_DFIRSTNAME = "dfirstname"; 
    public static final String KEY_DSURNAME = "dsurname"; 
    public static final String KEY_DBUILDING= "dbuilding"; 
    public static final String KEY_DSTREET= "dstreet"; 
    public static final String KEY_DCITY = "dcity"; 
    public static final String KEY_DCOUNTY = "dcounty"; 
    public static final String KEY_DCOUNTRY = "dcountry"; 
    public static final String KEY_DPOSTCODE= "dpostcode"; 
    public static final String KEY_LATITUDE = "latitude"; 
    public static final String KEY_LONGITUDE= "longitude"; 
    public static final String KEY_STATUS= "status"; 
    private static final String TAG = "DBAdapter"; 

    private static final String DATABASE_NAME = "fypdb"; 
    private static final String DATABASE_TABLE = "packagetable"; 
    private static final int DATABASE_VERSION = 2; 

    private static final String DATABASE_CREATE = "CREATE TABLE packagetable (_id integer primary key autoincrement, " 
     + "pfirstname text not null, psurname text not null, pbuilding text not null, " 
     + "pstreet text not null, pcity text not null, pcounty text not null, " 
     + "pcountry text not null, ppostcode text not null, dfirstname text not null, " 
     + "dsurname text not null, dbuilding text not null, dstreet text not null, " 
     + "dcity text not null, dcounty text not null, dcountry text not null, " 
     + "dpostcode text not null, latitude text not null, longitude text not null, " 
     + "status text not null);"; 

    private final Context context; 

    private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 

    public DBAdapter(Context ctx) 
    { 
     this.context = ctx; 
     DBHelper = new DatabaseHelper(context); 
    } 

    private static class DatabaseHelper extends SQLiteOpenHelper 
    { 
     DatabaseHelper(Context context) 
     { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) 
     { 
      db.execSQL(DATABASE_CREATE); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, 
           int newVersion) 
     { 
      Log.w(TAG, "Upgrading database from version " + oldVersion 
        + " to " 
        + newVersion + ", which will destroy all old data"); 
      db.execSQL("DROP TABLE IF EXISTS packagetable"); 
      onCreate(db); 
     } 
    } 
    //---opens the database--- 
    public DBAdapter open() throws SQLException 
    { 
     db = DBHelper.getWritableDatabase(); 
     return this; 
    } 

    //---reads the database--- 
    public DBAdapter read() throws SQLException 
    { 
     db = DBHelper.getReadableDatabase(); 
     return this; 
    } 


    //---closes the database---  
    public void close() 
    { 
     DBHelper.close(); 
    } 

    //---insert a title into the database--- 
    public long insertPackage(String pfirstname, String psurname, 
      String pbuilding, String pstreet, String pcity, String pcounty, 
      String pcountry, String ppostcode, String dfirstname, 
      String dsurname, String dbuilding, String dstreet, String dcity, 
      String dcounty, String dcountry, String dpostcode, 
      String longitude, String latitude, String status){ 

     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_PFIRSTNAME, pfirstname); 
     initialValues.put(KEY_PSURNAME, psurname); 
     initialValues.put(KEY_PBUILDING, pbuilding); 
     initialValues.put(KEY_PSTREET, pstreet); 
     initialValues.put(KEY_PCITY, pcity); 
     initialValues.put(KEY_PCOUNTY, pcounty); 
     initialValues.put(KEY_PCOUNTRY, pcountry); 
     initialValues.put(KEY_PPOSTCODE, ppostcode); 
     initialValues.put(KEY_DFIRSTNAME, dfirstname); 
     initialValues.put(KEY_DSURNAME, dsurname); 
     initialValues.put(KEY_DBUILDING, dbuilding); 
     initialValues.put(KEY_DSTREET, dstreet); 
     initialValues.put(KEY_PCITY, pcity); 
     initialValues.put(KEY_DCOUNTY, dcounty); 
     initialValues.put(KEY_DCOUNTRY, dcountry); 
     initialValues.put(KEY_DPOSTCODE, dpostcode); 
     initialValues.put(KEY_LONGITUDE, longitude); 
     initialValues.put(KEY_LATITUDE, latitude); 
     initialValues.put(KEY_STATUS, status); 

     return db.insert(DATABASE_TABLE, null, initialValues); 
    } 

    //---deletes a particular title--- 
    public boolean deletePackage(long rowId) 
    { 
     return db.delete(DATABASE_TABLE, KEY_ROWID + 
       "=" + rowId, null) > 0; 
    } 

    //---retrieves all the titles--- 
    public Cursor getAllPackages() 
    { 
     return db.query(DATABASE_TABLE, new String[] { 
       KEY_ROWID, 
       KEY_PFIRSTNAME, 
       KEY_PSURNAME, 
       KEY_PBUILDING, 
       KEY_PSTREET, 
       KEY_PCITY, 
       KEY_PCOUNTY, 
       KEY_PCOUNTRY, 
       KEY_PPOSTCODE, 
       KEY_DFIRSTNAME, 
       KEY_DSURNAME, 
       KEY_DBUILDING, 
       KEY_DSTREET, 
       KEY_DCITY, 
       KEY_DCOUNTY, 
       KEY_DCOUNTRY, 
       KEY_DPOSTCODE, 
       KEY_LATITUDE, 
       KEY_LONGITUDE, 
       KEY_STATUS, 
       }, 
       null, 
       null, 
       null, 
       null, 
       null); 

    } 

    //---retrieves a particular title--- 
    public Cursor getPackage(String rowId) throws SQLException 
    { 
     Cursor mCursor = 
       db.query(true, DATABASE_TABLE, new String[] { 
         KEY_ROWID, 
         KEY_PFIRSTNAME, 
         KEY_PSURNAME, 
         KEY_PBUILDING, 
         KEY_PSTREET, 
         KEY_PCITY, 
         KEY_PCOUNTY, 
         KEY_PCOUNTRY, 
         KEY_PPOSTCODE, 
         KEY_DFIRSTNAME, 
         KEY_DSURNAME, 
         KEY_DBUILDING, 
         KEY_DSTREET, 
         KEY_DCITY, 
         KEY_DCOUNTY, 
         KEY_DCOUNTRY, 
         KEY_DPOSTCODE, 
         KEY_LATITUDE, 
         KEY_LONGITUDE, 
         KEY_STATUS 
         }, 
         KEY_ROWID + "=" + rowId, 
         null, 
         null, 
         null, 
         null, 
         null); 
     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
     return mCursor; 
    } 

    //---updates a package location--- 
    public boolean updateLocation(long rowId, String longitude, String latitude) 
    { 
     ContentValues args = new ContentValues(); 
     args.put(KEY_LONGITUDE, longitude); 
     args.put(KEY_LATITUDE, latitude);; 
     return db.update(DATABASE_TABLE, args, 
         KEY_ROWID + "=" + rowId, null) > 0; 
    } 

    //---updates a package status--- 
    public boolean updateStatus(long rowId, String status) 
    { 
     ContentValues args = new ContentValues(); 
     args.put(KEY_STATUS, status); 
     return db.update(DATABASE_TABLE, args, 
         KEY_ROWID + "=" + rowId, null) > 0; 
    } 
} 

這是一種嘗試插入一些數據我的包表的代碼:

package com.fyp; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 

public class ScreenNewPackage2 extends Activity { 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.newpackage2); 

     DBAdapter db = new DBAdapter(this); 

     db.open();   
     long id; 
     id = db.insertPackage(
        "John", 
        "Doe", 
        "1", 
        "Imagination Road", 
        "Super Town", 
        "Berkshire", 
        "UK", 
        "AB12CD", 
        "Mary", 
        "Doe", 
        "24", 
        "Invisible Street", 
        "Large City", 
        "Berkshire", 
        "UK", 
        "AB89CD", 
        "51.43848", 
        "-0.944492", 
        "Picked up"); 

       Button newPackage2CancelButton = (Button) findViewById(R.id.NewPackage2_Cancel_Button); 
       newPackage2CancelButton.setOnClickListener(new OnClickListener() { 
        public void onClick(View v) { 
         Intent i = new Intent(); 
         i.setClassName("com.fyp", "com.fyp.ScreenNewPackage1"); 
         startActivity(i); 
         finish(); 
      } 
     }); 

     Button newPackage2NextButton = (Button) findViewById(R.id.NewPackage2_Next_Button); 
     newPackage2NextButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 

       Intent i = new Intent(); 
       i.setClassName("com.fyp", "com.fyp.ScreenMenu"); 
       startActivity(i); 
       finish(); 
      } 
     }); 
    } 
} 

,這裏是錯誤消息:

03-02 16:54:12.139: ERROR/Database(25235): Error inserting dcountry=UK dcounty=Berkshire pfirstname=John pcounty=Berkshire status=Picked up pcountry=UK dbuilding=24 pstreet=Imagination Road psurname=Doe dstreet=Invisible Street pbuilding=1 dpostcode=AB89CD ppostcode=AB12CD longitude=51.43848 latitude=-0.944492 pcity=Super Town dsurname=Doe dfirstname=Mary 
03-02 16:54:12.139: ERROR/Database(25235): android.database.sqlite.SQLiteException: table packagetable has no column named psurname: , while compiling: INSERT INTO packagetable(dcountry, dcounty, pfirstname, pcounty, status, pcountry, dbuilding, pstreet, psurname, dstreet, pbuilding, dpostcode, ppostcode, longitude, latitude, pcity, dsurname, dfirstname) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?); 
03-02 16:54:12.139: ERROR/Database(25235):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 
03-02 16:54:12.139: ERROR/Database(25235):  at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) 
03-02 16:54:12.139: ERROR/Database(25235):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64) 

任何幫助將不勝感激,我沒有嘗試過的sqlite3命令行運行,因爲我不知道該怎麼做等等,甚至是閱讀各種文章/教程之後。

更新:2011年3月3日 - 新的錯誤消息。

感謝布魯默識別一個問題,我現在運行上面的編輯的代碼時,收到以下錯誤:

似乎
03-02 17:31:27.849: ERROR/Database(25517): Error inserting dcountry=UK dcounty=Berkshire pfirstname=John pcounty=Berkshire status=Picked up pcountry=UK dbuilding=24 pstreet=Imagination Road psurname=Doe dstreet=Invisible Street pbuilding=1 dpostcode=AB89CD ppostcode=AB12CD longitude=51.43848 latitude=-0.944492 pcity=Super Town dsurname=Doe dfirstname=Mary 
03-02 17:31:27.849: ERROR/Database(25517): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed   
03-02 17:31:27.849: ERROR/Database(25517):  at android.database.sqlite.SQLiteStatement.native_execute(Native Method)   
03-02 17:31:27.849: ERROR/Database(25517):  at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55) 

約束錯誤,任何想法將不勝感激。

回答

0

您的數據庫表沒有列「psurname」,但您嘗試向其中插入數據。您需要查看錶的結構,然後查找其中存在的名稱,或者添加該列(如果該列尚不存在)。

+0

我以爲DBAdapter會使用CREATE_DATABASE創建表和列? – Greea001 2011-03-02 17:17:55

+0

啊,我在DBAdapter中看到一個與psurname有關的錯字,現在已經被修改,現在收到一個約束錯誤信息,我上面已經編輯過來反映這個改變。感謝您找出其中一個問題:) – Greea001 2011-03-02 17:34:42

+0

您使用什麼字段作爲主鍵?這個錯誤很可能表明你試圖在這個字段中插入一個已經存在於另一個記錄中的值。主鍵字段在記錄中必須是唯一的。 – Blumer 2011-03-02 17:46:38

相關問題