在SQLite中插入记录时返回-1

时间:2010-08-20 10:29:37

标签: android sqlite

这是我的SimpleDBAdapter类,它封装了访问数据库的所有复杂性,而内部类SimpleDBHelper负责CRUD操作,现在我面临的问题是当我部署代码时,表格被创建,但是该 我无法将值插入表中,id返回-1表示插入值时出现错误。

id = db.insert(TABLE_SIMPLETABLE_CLIENT1,null,contentvalues);

SimpleDBAdapter.java

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.view.ViewGroup.MarginLayoutParams;

public class SimpleDBAdapter { // Define all the constants that are to used in the Program private static final String DATABASE_NAME = "SimpleDB.db"; private static final String TABLE_SIMPLETABLE_CLIENT1 = "SimpleTable1"; private static final int DATABASE_VERSION = 1;

private static final String KEY_EMPLOYEE_ID = "Employee_id";
private static final String KEY_EMPLOYEE_NAME = "Name";
private static final String KEY_EMPLOYEE_DESIGNATION = "Designation";
private static final String KEY_EMPLOYEE_MARITUALSTATUS = "Maritual Status";
private static final String KEY_EMPLOYEE_DOJ = "Date of Joining";

private static final String TAG = "SimpleDBAdapter";

String CREATE_TABLE_SIMPLETABLE = "CREATE TABLE IF NOT EXISTS "
    + TABLE_SIMPLETABLE_CLIENT1
    + " (Employee_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
    + "  Employee_Name VARCHAR(20)," + "  JoinedDateTime DATETIME,"
    + "  Designation VARCHAR(20),"
    + "  Maritual_Status BOOLEAN DEFAULT 'FALSE');";

private final Context ctx;
private SQLiteDatabase db;
private SimpleDBHelper simpledbhelper;

public SimpleDBAdapter(Context context) {
Log.i(TAG,"SimpleDBAdapter Constructor");
 ctx = context;
 simpledbhelper = new SimpleDBHelper(ctx);
}

  class SimpleDBHelper extends SQLiteOpenHelper {
    private static final String TAG = "SimpleDBHelper";
    public SimpleDBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.i(TAG,"Calling.. super(context, DATABASE_NAME, null, DATABASE_VERSION)");
    }   

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i(TAG,"Creating Table named CREATE_TABLE_SIMPLETABLE");
        db.execSQL(CREATE_TABLE_SIMPLETABLE);
           }



    @Override
    public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
        Log.w(TAG, "Upgarding from" + oldversion + "to" + newversion
                + "could remove all the old data");
        db.execSQL("DROP TABLE IF EXISTS TABLE_SIMPLETABLE_CLIENT1");
        onCreate(db);
    }
}

//Opens the DB
public void open() throws SQLException {
    Log.i(TAG,"Open() Called...");
    db = simpledbhelper.getWritableDatabase();
    simpledbhelper.onCreate(db);

}

//closes the DB
public void close() {
    Log.i(TAG,"Close() Called...");
    simpledbhelper.close();
}

//inserting the values in to Table
public long insertEntry(String Name,String Designation,Boolean MaritualStatus,String date){
    Log.i(TAG,"Insert Entry ()");
    long id = 0;
    try{
    ContentValues contentvalues = new ContentValues();
    contentvalues.put(KEY_EMPLOYEE_NAME, Name);
    contentvalues.put(KEY_EMPLOYEE_DOJ,date);
    contentvalues.put(KEY_EMPLOYEE_DESIGNATION, Designation);
    contentvalues.put(KEY_EMPLOYEE_MARITUALSTATUS, MaritualStatus);
    id = db.insert(TABLE_SIMPLETABLE_CLIENT1,null,contentvalues);
    }
    catch(Exception err){
        Log.i(TAG,"Error Encountered...");
        Log.e(TAG,String.valueOf(err).toString());
    }
    return id;
}

}

In the MainClass file (DBHandler.java), i am creating instance of the code and calling the insert method of this class as follows,

DBHandler.java

import android.app.Activity; import android.os.Bundle; import android.util.Log; public class SimpleDBApplication extends Activity { private static final String TAG = "SimpleDBApplication"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); SimpleDBAdapter DBAdapter = new SimpleDBAdapter(this); Log.i(TAG,"Called SimpleDBAdapter"); //call to open the DB DBAdapter.open(); long id; // add an entry id = DBAdapter.insertEntry("Vinayaka","CEO",false,"17-7-2010"); Log.w(TAG,"Tuple Inserted"); Log.w(TAG," The Value of ID :" +String.valueOf(id).toString()); //call to close the DB DBAdapter.close(); } }

提前致谢...

2 个答案:

答案 0 :(得分:2)

您可以获取具体原因,如果您使用insert()代替insertOrThrow()并捕获异常,则错误消息应该可以帮助您快速找出原因。

答案 1 :(得分:1)

到目前为止,我看到你的ContentValues键与列名不同,这应该是我记得的。

所以你的常数必须成为;

private static final String KEY_EMPLOYEE_ID = "Employee_ID";
private static final String KEY_EMPLOYEE_NAME = "Employee_Name";
private static final String KEY_EMPLOYEE_DESIGNATION = "Designation";
private static final String KEY_EMPLOYEE_MARITUALSTATUS = "Maritual_Status";
private static final String KEY_EMPLOYEE_DOJ = "JoinedDateTime";

如果这不能解决您的问题,请尝试发布堆栈跟踪,以便我们提供更多详细信息来帮助您。