【发布时间】:2020-04-09 14:31:52
【问题描述】:
我是 android 编程和使用 SQLite 的新手。我正在尝试插入一个名为“menu”的数据库,该数据库有一个名为“items”的表。该表的属性是“名称”和“价格”,它们分别是文本和实数。 (我想为所有代码道歉,但我认为这些代码会有用)。我的“MySQLiteHelper”类这样创建表:
package com.example.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLiteHelper extends SQLiteOpenHelper {
//Creating the items table
public static final String TABLE_ITEMS = "items";
public static final String ITEMS_COLUMN_NAME = "name";
public static final String ITEMS_PRICE = "price";
private static final String DATABASE_NAME = "menu";
private static final int DATABASE_VERSION = 1;
// create table comments(
// _id integer primary key autoincrement,
// comment text not null );
private static final String TABLE_CREATE_ITEMS = "create table "
+ TABLE_ITEMS + "( " + ITEMS_COLUMN_NAME + " text primary key, "
+ ITEMS_PRICE + " real not null );";
public MySQLiteHelper( Context context ) {
super( context, DATABASE_NAME, null, DATABASE_VERSION );
}
@Override
public void onCreate( SQLiteDatabase database ) {
//Create the items table
database.execSQL( TABLE_CREATE_ITEMS );
//Create the combos table
//database.execSQL(TABLE_CREATE_COMBO);
}
@Override
public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion ) {
Log.w( MySQLiteHelper.class.getName( ), "Upgrading database from version "
+ oldVersion + " to " + newVersion + ", which will destroy all old data" );
db.execSQL( "DROP TABLE IF EXISTS" + TABLE_CREATE_ITEMS );
//db.execSQL( "DROP TABLE IF EXISTS" + TABLE_CREATE_COMBO );
onCreate( db );
}
}
我可以确认数据库“菜单”是在 data/data/package/databases 文件夹中创建的。虽然我无法使用 adb shell 连接到 SQLite,所以我无法确认表模式。单击插入按钮后,我的 datasource.createComment(String, Float) 被调用:
public void onClick (View v) {
@SuppressWarnings("unchecked")
//Load up the current values in the adapter into this adapter object
ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>)getListAdapter();
//Set comment to null
Comment comment = null;
//Check to see which button was pressed
switch (v.getId()) {
case R.id.insert:
String[] option = new String[] {"One", "Two", "Three"};
int index = new Random().nextInt(3);
//When inserting comment you just have to pass value
comment = datasource.createComment("TEST", (float) 12.30);
adapter.add(comment); //Adding to listview
break;
case R.id.delete:
if(getListAdapter().getCount() > 0) {
comment = (Comment)getListAdapter().getItem(0);
adapter.remove(comment);//removing from listview
datasource.deleteComment(comment); //delete from db.
}
break;
}
//Updating the adapter
adapter.notifyDataSetChanged();
}
通过一些调试(打印语句),我可以确认 database.insert 返回 -1。插入不成功。在我的 Logcat 中打印出以下错误
12-16 23:42:30.002 11063-11063/com.example.sqlite E/SQLiteDatabase: Error inserting price=12.3 name=TEST
android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:113)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
at com.example.sqlite.CommentsDataSource.createComment(CommentsDataSource.java:47)
at com.example.sqlite.InsertItems.onClick(InsertItems.java:50)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3039)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
我的“CommentsDataSource”的代码如下。请忽略 createComment() 之后的方法。我正在一次编辑我的教授代码一种方法。
public class CommentsDataSource {
// Database fields
//Used toe xecute commands on db.
private SQLiteDatabase database;
//Opening up the mysqlhelper which is in charge of opening up connection and maintaining
private MySQLiteHelper dbHelper;
//Holds all the values from the database
private String[] allColumnsItems = { MySQLiteHelper.ITEMS_COLUMN_NAME,
MySQLiteHelper.ITEMS_PRICE };
//Creating the default constructor
public CommentsDataSource(Context context) {
//Give context to the class that manages the connection
dbHelper = new MySQLiteHelper(context);
}
//Opening up the connction to the db
public void open() throws SQLException {
//Open the db for reading and writing
database = dbHelper.getWritableDatabase(); //getWritableDatabase is what opens the connection
}
//Closing the database
public void close() throws SQLException {
dbHelper.close();
}
//Creating and commiting comment
public Comment createComment(String comment, float price) {
//Insert into comments values('')
ContentValues values = new ContentValues();
//Load the column id and value into ContentValues
values.put(MySQLiteHelper.ITEMS_COLUMN_NAME, comment);
values.put("price", price);
//Now insert into table
long insertID = database.insert(MySQLiteHelper.TABLE_ITEMS, null, values);
if(insertID == -1) {
System.out.println("JSDLFJLSJDFL");
}
//Create a cursor to keep track of results. When creating cursors they equal database.query
Cursor cursor = database.query(MySQLiteHelper.TABLE_ITEMS, allColumnsItems, null, null, null, null, null);
//Move cursor tot he front
cursor.moveToFirst();
//Return the comment which is added to listview
return cursorToComment(cursor);
}
public void deleteComment(Comment comment) {
// select id, comment from comments;
long id = comment.getId();
//Debug purposes
System.out.println("Comment to be deleted: " + id);
//Deleting from db
database.delete(MySQLiteHelper.TABLE_ITEMS, MySQLiteHelper.ITEMS_COLUMN_NAME + "=" + comment, null);
}
public List<Comment> getAllComments() {
//Select * from comments
List<Comment> comments = new ArrayList<Comment>();
//Fetching the results of query into cursor
Cursor cursor = database.query(MySQLiteHelper.TABLE_ITEMS, allColumnsItems, null, null, null, null, null);
//Move to front of cursor
cursor.moveToFirst();
//Iterate through cursor
while(!cursor.isAfterLast()) {
Comment comment = cursorToComment(cursor);
//Add results to arraylist
comments.add(comment);
cursor.moveToNext();
}
cursor.close();
return comments;
}
public Comment cursorToComment(Cursor cursor) {
Comment comment = new Comment();
comment.setId(cursor.getLong(0));
comment.setComment(cursor.getString(1));
return comment;
}
}
我正在努力找出我的错误在哪里。我的约束应该都是有效的?
【问题讨论】: