我想在sqlite数据库中插入一条新记录。
我的表称为class MyPageControl: UIPageControl {
override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {
super.sendAction(action, to: target, for: event)
updateSomething(for: currentPage)
}
}
。它具有列words
,id
,word
,meaning
,details
,lesson
列。
当我尝试插入新记录时,它只是将ticks
保存为details
,而将word
保存为lesson
,而meaning
和{{1} }以空值存储。
自动增量主键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(){
}
类上创建方法: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();
}
答案 0 :(得分:1)
只需将数据库版本更新为更高版本(而不是1,请使用5)。并检查它。在运行应用程序之前,请先卸载然后再运行。
答案 1 :(得分:0)
使用
db.insert(YOUR_TABLE_NAME,values);
代替
db.execSQL()
功能