我是大学IT学生的最后一年,我在尝试在我的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日 - 新错误消息。
感谢Blumer识别出一个问题,我现在在运行上面编辑过的代码时收到以下错误:
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 :(得分:0)
您的数据库表没有“psurname”列,但您正在尝试将数据插入其中。您需要查看表的结构,并找出其中的名称或添加列(如果该列尚不存在。)