【发布时间】:2011-04-01 14:19:00
【问题描述】:
这是我的SimpleDBAdapter Class,它封装了访问数据库的所有复杂性,内部类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;
}
}
在MainClass文件(DBHandler.java)中,我正在创建代码实例并调用该类的insert方法,如下所示,
DBHandler.java
导入android.app.Activity; 导入android.os.Bundle; 导入android.util.Log; 公共类 SimpleDBApplication 扩展 Activity { 私有静态最终字符串 TAG = "SimpleDBApplication"; /** 在第一次创建活动时调用。 */ @覆盖 公共无效 onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 设置内容视图(R.layout.main); SimpleDBAdapter DBAdapter = new SimpleDBAdapter(this); Log.i(TAG,"称为 SimpleDBAdapter"); //调用打开数据库 DBAdapter.open(); 长身份证; // 添加一个条目 id = DBAdapter.insertEntry("Vinayaka","CEO",false,"17-7-2010"); Log.w(TAG,"已插入元组"); Log.w(TAG," ID 的值:" +String.valueOf(id).toString()); //调用关闭数据库 DBAdapter.close(); } }在此先感谢...
【问题讨论】: