我想在sqlite数据库中插入一条新记录。
我的表称为words
。它具有列id
,word
,meaning
,details
,lesson
,ticks
。
当我尝试插入新记录时,它只是将details
保存为word
,并且将lesson
保存为meaning
,而details
和lesson
存储为空值。
自动增量主键id
和ticks
正确存储。
码
处理程序:
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
protected static final String DATABASE_NAME = "wordDatabase";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE words " +
"( id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"word TEXT, " +
"meaning TEXT, " +
"details TEXT, " +
"lesson TEXT, " +
"ticks INTEGER ) ";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS words";
db.execSQL(sql);
onCreate(db);
}
宾语:
public class ObjectStudent {
int id;
String word;
String meaning;
String details;
String lesson;
int ticks;
public ObjectStudent(){
}
OnClickListener:
public class OnClickeListenerCreateStudent implements View.OnClickListener {
@Override
public void onClick(View view) {
final Context context = view.getRootView().getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View formElementsView = inflater.inflate(R.layout.student_input_form, null, false);
final EditText et_word = (EditText) formElementsView.findViewById(R.id.et_word);
final EditText et_meaning = (EditText) formElementsView.findViewById(R.id.et_meaning);
final EditText et_details = (EditText) formElementsView.findViewById(R.id.et_details);
final EditText et_lesson = (EditText) formElementsView.findViewById(R.id.et_lesson);
new AlertDialog.Builder(context)
.setView(formElementsView)
.setTitle("Create Word")
.setPositiveButton("Add",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String wordTitle = et_word.getText().toString();
String wordMeaning = et_meaning.getText().toString();
String wordDetails = et_details.getText().toString();
String wordLesson = et_lesson.getText().toString();
ObjectStudent objectStudent = new ObjectStudent();
/* objectStudent.word= wordTitle;
objectStudent.meaning= wordMeaning;
objectStudent.details= wordDetails;
objectStudent.lesson= wordLesson;*/
objectStudent.word = "word"; // et_word.getText().toString();
objectStudent.meaning = "meaning"; // et_meaning.getText().toString();
objectStudent.details = "details"; // et_details.getText().toString();
objectStudent.lesson = "lesson"; // et_lesson.getText().toString();
objectStudent.ticks= 1;
boolean createSuccessful = new TableControllerStudent(context).create(objectStudent);
if(createSuccessful){
Toast.makeText(context, "Word information was saved.", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Unable to save Word information.", Toast.LENGTH_SHORT).show();
}
((MainActivity) context).countRecords();
((MainActivity) context).readRecords();
dialog.cancel();
}
}).show();
}
在
TableControllerStudent
类上创建方法:public class TableControllerStudent extends DatabaseHandler {
public TableControllerStudent(Context context) {
super(context);
}
public boolean create(ObjectStudent objectStudent) {
ContentValues values = new ContentValues();
/* values.put("word", objectStudent.word);
values.put("meaning", objectStudent.meaning);
values.put("details", objectStudent.details);
values.put("lesson", objectStudent.lesson);*/
values.put("word", "w");
values.put("meaning", "m");
values.put("details", "d");
values.put("lesson", "l");
values.put("ticks", objectStudent.ticks);
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("INSERT INTO " + "words "+ "(word, meaning,details, lesson, ticks ) VALUES ('word','meaning','details','lesson',2)");
//boolean createSuccessful = db.insert("words", null, values) > 0;
db.close();
//return createSuccessful;
return true;
}
最佳答案
只需将数据库版本更新为更高版本(而不是1,使用5)。并检查它。在运行应用程序之前,请先卸载然后再运行。