gpt4 book ai didi

java - 数据未添加到数据库 SQLITE

转载 作者:行者123 更新时间:2023-12-01 22:27:12 24 4
gpt4 key购买 nike

我试图在 SQLITE 数据库中插入数据,但当我打开数据库时数据不存在。

我正在尝试开发一个能够在数据库中存储数据的应用程序。但是当我尝试添加数据时,它不会添加到表中。以下是数据库助手和我调用插入数据函数的 Activity 的代码:

公共(public)类DatabaseHelper扩展了SQLiteOpenHelper{

public static final String DATABASE_NAME = "Channels.db" ;
public static final String TABLE_NAME = "Channels_table" ;
public static final String COL_1 = "Channel_number" ;
public static final String COL_2 = "Channel_name" ;


public DatabaseHelper(Context context){
super(context, DATABASE_NAME , null , 1);

}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (Channel_number INTEGER PRIMARY KEY , Channel_name TEXT )");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

public boolean insertData(int c_number , String c_name){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1 , c_number);
contentValues.put(COL_2 , c_name);

long result = db.insert(TABLE_NAME , null , contentValues);

if (result == -1){
return false;
}
else
return true;
}

公共(public)类 Add_Activity 扩展 AppCompatActivity {

DatabaseHelper db;

EditText channel_name , channel_number ;
Button Add_button;

protected void onCreate (Bundle savedInstanceBundle) {
super.onCreate(savedInstanceBundle);
setContentView(R.layout.add_view);

channel_name = findViewById(R.id.channel_name_textview);
channel_number = findViewById(R.id.channel_number_textview);
Add_button = (Button) findViewById(R.id.add_button);

db = new DatabaseHelper(this);

Button_tapped();

}

public void Button_tapped(){
Add_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(channel_name.getText().toString().isEmpty() || channel_number.getText().toString().isEmpty()){
Toast.makeText(Add_Activity.this , getText(R.string.empty_fields) , Toast.LENGTH_LONG).show();
}
else if (Integer.parseInt(channel_number.getText().toString()) < 0 || Integer.parseInt(channel_number.getText().toString()) > 84){
Toast.makeText(Add_Activity.this , getText(R.string.invalid_channel_numb) , Toast.LENGTH_LONG).show();
channel_number.setText("");
}
else {
boolean insertdata = db.insertData(Integer.parseInt(channel_number.getText().toString()) , channel_name.getText().toString());

if (insertdata){
Toast.makeText(Add_Activity.this , getText(R.string.successfull_insertion) , Toast.LENGTH_LONG).show();
}
else
Toast.makeText(Add_Activity.this , getText(R.string.unsuccessfull_insertion) , Toast.LENGTH_LONG).show();

channel_number.setText("");
channel_name.setText("");
}
}
});
}

}

当我点击按钮时,我收到了插入不成功的消息,并且数据没有被插入到数据库中。我需要的是能够看到成功插入toast并在数据库中找到数据。

最佳答案

试试这个

  db.execSQL("CREATE TABLE " + TABLE_NAME + " ( " + COL_1 + " INTEGER PRIMARY KEY, " + COL_2 + " TEXT )");

而不是

db.execSQL("create table " + TABLE_NAME + " (Channel_number INTEGER PRIMARY KEY , Channel_name TEXT )");

关于java - 数据未添加到数据库 SQLITE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58563943/

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