我一直在努力奋斗数小时。我的SQLite插入总是返回-1 ...
以下是我在一个文件中的主要活动的代码,该文件在另一个文件中调用我的db代码:
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
}
这是我在另一个文件中的数据库适配器代码:
public class 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(?,?,?);
我将不胜感激任何帮助!
答案 0 :(得分:0)
有必要为表“_id”命名主id字段,以便Android知道绑定表的id字段的位置。因此,我建议您尝试将ID列命名为“_id”而不是“jobid”。
另外,我建议不要在只进行一次操作的情况下使用事务(以后可以决定将多个插入包装到事务中)。
答案 1 :(得分:0)
DATABASE_CREATE语句中存在错误,因此未创建表。这就是你收到错误信息的原因。尝试修复您的create语句(打开括号后删除加号):
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);";
答案 2 :(得分:0)
您的create语句错误。这是正确的:
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);";
如果您尝试插入与约束不匹配的行(例如,已存在的主键或JOB_TELE / JOB_TEXT列的空值),则会出现异常。
< / LI> 醇>