gpt4 book ai didi

java - 在android studio中将新记录插入sqlite时出现问题

转载 作者:行者123 更新时间:2023-12-02 09:51:34 25 4
gpt4 key购买 nike

我想将一条新记录插入到 sqlite 数据库中。我的表名为words。它具有列 idwordmeaningdetailslesson刻度

当我尝试插入新记录时,它只是将details保存为word,将lesson保存为meaning ,而 detailslesson 则使用 null 值存储。自动增量主键idticks存储正确。

<小时/>

代码

处理程序:

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)。并检查它。在运行应用程序之前,请卸载然后运行。

关于java - 在android studio中将新记录插入sqlite时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56292015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com