我在 sqlite 数据库中有两个表。第一个获取岸上的数据输入。当我尝试将数据输入到第二个表(设置)时,出现错误:
05-20 14:25:04.941: E/Database(352): Error inserting uri=null gender=female
age_month=12 age_day=05 age_year=1190 name=Tom
05-20 14:25:04.941: E/Database(352): android.database.sqlite.SQLiteConstraintException:
将数据插入表中的 Main 活动方法是:
public class Choose extends Activity implements OnClickListener, OnCheckedChangeListener{
private static final int SELECT_PICTURE = 1;
static String selected ImagePath, Gender, BDYear, BDMonth, BDDay, bfNameת;
EditText bName;
TextView outPut;
Button saveSettings, browseImageBtn;
RadioGroup genderGroup;
RadioButton male, female;
DatePicker bBornDate;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choose_image);
bName = (EditText) findViewById(R.id.editText1);
saveSettings = (Button) findViewById(R.id.settingsSaveBtn);
browseImageBtn = (Button) findViewById(R.id.chooseimg);
genderGroup = (RadioGroup) findViewById(R.id.genderGroup);
male = (RadioButton) findViewById(R.id.Male);
female = (RadioButton) findViewById(R.id.Female);
bBornDate = (DatePicker) findViewById(R.id.bBorndatePicker);
outPut = (TextView) findViewById(R.id.settingsOutPutText);
genderGroup.setOnCheckedChangeListener(Choose.this);
saveSettings.setOnClickListener(Choose.this);
browseImageBtn.setOnClickListener(Choose.this);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
String ipath = selectedImagePath;
public void onClick(View v) {
if (browseImageBtn == v){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
}else if(saveSettings == v){
BDYear = "1190";
BDMonth = "12";
BDDay = "05";
String bfName = "Tom";
String pt = ipath;
String bn = bfName;
String bg = Gender;
String bbdy = BDYear;
String bbdm = BDMonth;
String bbdd = BDDay;
DatBas settingentry = new DatBas(Choose.this);
settingentry.open();
settingentry.createEntrySettings(pt, bn, bg, bbdy, bbdm, bbdd);
settingentry.close();
// Intent myIntent = new Intent(Choose.this, Tamar_appActivity.class);
// Choose.this.startActivity(myIntent);
//read the data
}
}
public void onCheckedChanged(RadioGroup arg0, int arg1) {
switch (arg1){
case R.id.Male:
Gender = "male";
break;
case R.id.Female:
Gender = "female";
break;
}
}
}
The DatBas Helper is:
public class DatBas {
public static final String KEY_ROWID = "_id";
public static final String KEY_SHOURS = "start_hour";
public static final String KEY_SMINUTE = "start_minute";
public static final String KEY_SDATE = "start_date";
public static final String KEY_AMOUNT = "amount";
public static final String KEY_SIDE = "side";
public static final String KEY_KIND = "kind";
public static final String KEY_ROW_ID = "_id2";
public static final String KEY_IMAGE_PATH = "uri";
public static final String KEY_NAME = "name";
public static final String KEY_GENDER = "gender";
public static final String KEY_BORN_DATE_YEAR = "age_year";
public static final String KEY_BORN_DATE_MONTH = "age_month";
public static final String KEY_BORN_DATE_DAY = "age_day";
private static final String DATABASE_NAME = "TamatDB";
private static final String DATABASE_TABLE = "stop_watch_records";
private static final String DATABASE_TABLE_SETTINGS = "settings";
private static final int DATABASE_VERSION = 3 ;
private TamarDatabase thdb;
private static Context tcontext;
private SQLiteDatabase tdb;
private static class TamarDatabase extends SQLiteOpenHelper {
public TamarDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String ctData = "CREATE TABLE " + DATABASE_TABLE + " ( "
+ KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_SHOURS + " TEXT NOT NULL, " + KEY_SMINUTE
+ " TEXT NOT NULL, " + KEY_SDATE + " TEXT NOT NULL, "
+ KEY_AMOUNT + " TEXT NOT NULL, " + KEY_SIDE
+ " TEXT NOT NULL, " + KEY_KIND + " TEXT NOT NULL );";
db.execSQL(ctData);
String ctSettings = "CREATE TABLE " + DATABASE_TABLE_SETTINGS
+ " ( " + KEY_ROW_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_IMAGE_PATH + " TEXT NOT NULL, " + KEY_NAME
+ " TEXT NOT NULL, " + KEY_GENDER + " TEXT NOT NULL, "
+ KEY_BORN_DATE_YEAR + " TEXT NOT NULL, "
+ KEY_BORN_DATE_MONTH + " TEXT NOT NULL, "
+ KEY_BORN_DATE_DAY + " TEXT NOT NULL);";
db.execSQL(ctSettings);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_SETTINGS);
onCreate(db);
}
}
public DatBas(Context c) {
tcontext = c;
}
public DatBas open() throws SQLiteException {
thdb = new TamarDatabase(tcontext);
tdb = thdb.getWritableDatabase();
return this;
}
public SQLiteDatabase getReadableDatabase() throws SQLiteException {
thdb = new TamarDatabase(tcontext);
tdb = thdb.getReadableDatabase();
return tdb;
}
public void close() {
tdb.close();
}
public long createEntry(String sh, String sm, String sd, String at,
String tside, String tkind) {
ContentValues cv = new ContentValues();
cv.put(KEY_SHOURS, sh);
cv.put(KEY_SMINUTE, sm);
cv.put(KEY_SDATE, sd);
cv.put(KEY_AMOUNT, at);
cv.put(KEY_SIDE, tside);
cv.put(KEY_SIDE, tkind);
return tdb.insert(DATABASE_TABLE, null, cv);
}
public long createEntrySettings(String pt, String bn, String bg,
String bbdy, String bbdm, String bbdd) {
ContentValues cv2 = new ContentValues();
cv2.put(KEY_IMAGE_PATH, pt);
cv2.put(KEY_NAME, bn);
cv2.put(KEY_GENDER, bg);
cv2.put(KEY_BORN_DATE_YEAR, bbdy);
cv2.put(KEY_BORN_DATE_MONTH, bbdm);
cv2.put(KEY_BORN_DATE_DAY, bbdd);
return tdb.insert(DATABASE_TABLE_SETTINGS, null, cv2);
}
public String getData() {
String[] columns = new String[] { KEY_ROWID, KEY_SHOURS, KEY_SMINUTE,
KEY_SDATE, KEY_AMOUNT, KEY_SIDE, KEY_KIND };
Cursor c = tdb.query(DATABASE_TABLE, columns, null, null, null, null,
null);
String results = "";
int iRaw = c.getColumnIndex(KEY_ROWID);
int iShours = c.getColumnIndex(KEY_SHOURS);
int iSminute = c.getColumnIndex(KEY_SMINUTE);
int iDate = c.getColumnIndex(KEY_SDATE);
int iAmount = c.getColumnIndex(KEY_AMOUNT);
int iSide = c.getColumnIndex(KEY_SIDE);
int iKind = c.getColumnIndex(KEY_KIND);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
results = results + "the id is " + c.getString(iRaw)
+ " the sart hour is " + " " + c.getString(iShours)
+ " the start minute is " + " " + c.getString(iSminute)
+ " the start date is " + " " + c.getString(iDate)
+ " the amount is " + " " + c.getString(iAmount)
+ " the side is " + " " + c.getString(iSide)
+ " the kind is " + " " + c.getString(iKind) + "\n";
}
return results;
}
public String getDataSettings() {
String[] columns = new String[] { KEY_ROW_ID, KEY_IMAGE_PATH,
KEY_NAME, KEY_GENDER, KEY_BORN_DATE_YEAR,
KEY_BORN_DATE_MONTH, KEY_BORN_DATE_DAY };
Cursor c = tdb.query(DATABASE_TABLE_SETTINGS, columns, null, null,
null, null, null);
String results = "";
int iRawId = c.getColumnIndex(KEY_ROW_ID);
int iBIPath = c.getColumnIndex(KEY_IMAGE_PATH);
int iBName = c.getColumnIndex(KEY_NAME);
int iGender = c.getColumnIndex(KEY_GENDER);
int iBBDateYear = c.getColumnIndex(KEY_BORN_DATE_YEAR);
int iBBDateMonth = c.getColumnIndex(KEY_BORN_DATE_MONTH);
int iBBDateDay = c.getColumnIndex(KEY_BORN_DATE_DAY);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
results = results + " the kind is " + " " + c.getString(iRawId)
+ " the kind is " + " " + c.getString(iBIPath)
+ " the kind is " + " " + c.getString(iBName)
+ " the kind is " + " " + c.getString(iGender)
+ " the kind is " + " " + c.getString(iBBDateYear)
+ " the kind is " + " " + c.getString(iBBDateMonth)
+ " the kind is " + " " + c.getString(iBBDateDay) + "\n";
}
return results;
}
public String getDataSettingsName() {
String[] columns = new String[] { KEY_ROW_ID, KEY_NAME };
Cursor c = tdb.query(DATABASE_TABLE_SETTINGS, columns, null, null,
null, null, null);
String results = "";
int iRawId = c.getColumnIndex(KEY_ROW_ID);
int iBName = c.getColumnIndex(KEY_NAME);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String[] resultsS = new String[] { c.getString(iRawId),
c.getString(iBName) };
}
return results;
}
public DatBas delete() {
tdb.delete(DATABASE_TABLE, null, null);
tdb.delete(DATABASE_TABLE_SETTINGS, null, null);
return null;
}
public static class TamarDatabaseCursor extends SQLiteCursor {
/** The query for this cursor */
private static final String QUERY = "SELECT _id2, uri, name, gender, age_year, age_month, age_day"
+ " FROM settings";
/** Cursor constructor */
private TamarDatabaseCursor(SQLiteDatabase db,
SQLiteCursorDriver driver, String editTable, SQLiteQuery query) {
super(db, driver, editTable, query);
}
/** Private factory class necessary for rawQueryWithFactory() call */
private static class Factory implements SQLiteDatabase.CursorFactory {
public Cursor newCursor(SQLiteDatabase db,
SQLiteCursorDriver driver, String editTable,
SQLiteQuery query) {
return new TamarDatabaseCursor(db, driver, editTable, query);
}
}
/* Accessor functions get one per database column */
public int getActressId() {
return getInt(getColumnIndexOrThrow("actress.actressId"));
}
}
public TamarDatabaseCursor getActress() {
SQLiteDatabase d = getReadableDatabase();
TamarDatabaseCursor c = (TamarDatabaseCursor) d.rawQueryWithFactory(
new TamarDatabaseCursor.Factory(), TamarDatabaseCursor.QUERY,
null, null);
c.moveToFirst();
return c;
}
}