2011-03-02 53 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