开发者

Android SQLite Exception table has no column

I'm a final year university IT Student and I'm having an issue trying to create a sqlite database within my android project. (I'm ok at programming but no expert, I've managed to find some code through various tutorials but I'm getting an error message when the code runs. It compiles fine and the rest of application works, just when it tries to put data into the database it causes an error.

Database Adapter code:

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

This is the code that tries to insert some data into my package table:

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

and here is the error message:

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)

Any help would be greatly appreciated, I haven't tried sqlite3 command run line as I have no idea how to do it etc, even after reading various articles/tutorials.

Update: 3rd March 2011 - New error message.

Thanks to Blumer for identifying one issue, I now receive the following error when running the edited code above:

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)

Constraint error it seems, any ideas would be much appreciated.


Your database table does not have a column "psurname", but you're trying to insert data into it. You'll need to look at the structure of your table and either find out what the name that is there is or add the column if it doesn't already exist.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜