【发布时间】:2011-05-05 04:22:13
【问题描述】:
我已经为此苦苦挣扎了好几个小时。我的 SQLite 插入总是返回 -1 ...
这是我在一个文件中的主要活动的代码,它在另一个文件中调用我的数据库代码:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
jobsdb = new NotifyJobsDBAdapter(this);
jobsdb.open();
currentjobid = 1;
currentjob = new Job(currentjobid, "Starting Tele Num", "Starting Text Msg");
currentjobid = jobsdb.insertJob(currentjob);
// this last statement returns -1
}
这是我在另一个文件中的数据库适配器代码:
公共类 NotifyJobsDBAdapter {
private static final String DB_NAME = "notifyjobsdb";
private static final String DB_TABLE = "notifyjobs";
private static final int DB_VERSION = 1;
private final Context ctx;
// Database column names
private static final String JOB_ID = "jobid";
private static final String JOB_TELE = "contactnumber";
private static final String JOB_TEXT = "textmessage";
private SQLiteDatabase notifyjobsdb;
private NotifyJobsDatabaseHelper dbHelper;
private boolean openness;
private boolean err;
private boolean err0;
private int cnt;
private int i1;
private String s1;
private String s2;
private static String s3;
private int res;
private SQLiteException ex0;
private SQLiteException ex1;
private static class NotifyJobsDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_CREATE =
"CREATE TABLE IF NOT EXISTS " + DB_TABLE +
" (+ " + JOB_ID + "INTEGER PRIMARY KEY, " +
JOB_TELE + "TEXT NOT NULL, " +
JOB_TEXT + "TEXT NOT NULL);";
private static final String DATABASE_UPGRADE =
"DROP TABLE IF EXISTS notifyjobs ";
public NotifyJobsDatabaseHelper(Context _context) {
super(_context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DATABASE_UPGRADE);
}
}
public NotifyJobsDBAdapter(Context _context) {
this.ctx = _context;
}
public void open() {
dbHelper = new NotifyJobsDatabaseHelper(ctx);
try { notifyjobsdb = dbHelper.getWritableDatabase();
} catch(SQLiteException ex) {
ex0 = ex;
}
}
public void close() {
notifyjobsdb.close();
}
//这是我的问题方法
public int insertJob(Job _job) {
ContentValues newJobValues = new ContentValues();
newJobValues.put(JOB_ID, _job.getjobid());
newJobValues.put(JOB_TELE, _job.gettelenum());
newJobValues.put(JOB_TEXT, _job.gettextmsg());
notifyjobsdb.beginTransaction();
// res is always returned a -1
try {
res = (int) notifyjobsdb.insertOrThrow(DB_TABLE, null, newJobValues);
} catch (SQLiteException ex) {
ex1 = ex;
}
notifyjobsdb.setTransactionSuccessful();
close();
return res;
这里,res 始终为 -1,调试器将 ex1 的详细信息显示为
"没有这样的表:notifyjobs:,编译时:INSERT INTO notifyjobs(contactnumber, textmessage, jobid) VALUES(?, ?, ?);
我将不胜感激!
【问题讨论】:
标签: android