Insert returns -1 with SQLite
I have been struggling with this for hours. My SQLite insert always returns -1 ...
Here is my code from the main activity in one file that calls my db code in another file:
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
}
This is my db adaptor code in another 开发者_运维百科file:
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();
}
// this is my problem method
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;
Here, res is always -1 and the debugger shows the detailmessage for ex1 as
"no such table: notifyjobs: , while compiling: INSERT INTO notifyjobs(contactnumber, textmessage, jobid) VALUES(?, ?, ?);
I would appreciate any help!
It is necessary to name the primary id field of your tables "_id" so Android will know where to bind the id field of your tables. So I suggest you try to name your ID columns "_id" instead of "jobid".
Also, I suggest not to use transactions where you do only one operation (you can later on decide to wrap multiple insert into a transaction).
Your create statement is wrong. This is the correct one:
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);";
If you're trying to insert a row that does not match a constraint (e.g. an already existing primary key or null values for JOB_TELE/JOB_TEXT column), you'll get an exception.
There is error in your DATABASE_CREATE statement, thus your table was not created. And that's why you're getting the error message. Try fixing your create statement (remove plus sign after opening bracket):
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);";
精彩评论