我试图在 SQLITE 数据库中插入数据,但当我打开数据库时它不存在。我正在尝试开发一个能够在数据库中存储数据的应用程序。但是当我尝试添加数据时,它不会添加到表中。以下是数据库助手和我调用插入数据函数的活动的代码:公共类 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);}@Overridepublic void onCreate(SQLiteDatabase db) { db.execSQL("create table " + TABLE_NAME + " (Channel_number INTEGER PRIMARY KEY , Channel_name TEXT )");}@Overridepublic 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;}公共类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();}当我点击按钮时,我收到了插入不成功的消息,并且数据没有被插入到数据库中。我需要的是能够看到成功插入 toast 并找到数据库中的数据。
1 回答
江户川乱折腾
TA贡献1851条经验 获得超5个赞
尝试这个
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 )");
添加回答
举报
0/150
提交
取消