填写表单字段,然后点击“添加产品”。按钮并收到以下错误:
01-20 19:24:08.539: E/SQLiteLog(16936): (14) cannot open file at line 32516 of [00bb9c9ce4]
01-20 19:24:08.539: E/SQLiteLog(16936): (14) os_unix.c:32516: (2) open(//ItemDatabase.db) -
01-20 19:24:08.544: E/SQLiteDatabase(16936): Failed to open database 'ItemDatabase.db'.
01-20 19:24:08.544: E/SQLiteDatabase(16936): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
01-20 19:24:08.544: E/SQLiteDatabase(16936): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:339)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:236)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:515)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:207)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:891)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:859)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at com.example.fooditemmonitor.AddItem.onClick(AddItem.java:89)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at android.view.View.performClick(View.java:4475)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at android.view.View$PerformClick.run(View.java:18786)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at android.os.Handler.handleCallback(Handler.java:730)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at android.os.Handler.dispatchMessage(Handler.java:92)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at android.os.Looper.loop(Looper.java:176)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at android.app.ActivityThread.main(ActivityThread.java:5419)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at java.lang.reflect.Method.invoke(Method.java:525)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
01-20 19:24:08.544: E/SQLiteDatabase(16936): at dalvik.system.NativeStart.main(Native Method)
01-20 19:24:08.544: D/AndroidRuntime(16936): Shutting down VM
01-20 19:24:08.544: W/dalvikvm(16936): threadid=1: thread exiting with uncaught exception (group=0x41c4d700)
01-20 19:24:08.554: E/AndroidRuntime(16936): FATAL EXCEPTION: main
01-20 19:24:08.554: E/AndroidRuntime(16936): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
01-20 19:24:08.554: E/AndroidRuntime(16936): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
01-20 19:24:08.554: E/AndroidRuntime(16936): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:339)
01-20 19:24:08.554: E/AndroidRuntime(16936): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:236)
01-20 19:24:08.554: E/AndroidRuntime(16936): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:515)
01-20 19:24:08.554: E/AndroidRuntime(16936): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:207)
01-20 19:24:08.554: E/AndroidRuntime(16936): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)
01-20 19:24:08.554: E/AndroidRuntime(16936): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:891)
01-20 19:24:08.554: E/AndroidRuntime(16936): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:859)
01-20 19:24:08.554: E/AndroidRuntime(16936): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
01-20 19:24:08.554: E/AndroidRuntime(16936): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671)
01-20 19:24:08.554: E/AndroidRuntime(16936): at com.example.fooditemmonitor.AddItem.onClick(AddItem.java:89)
01-20 19:24:08.554: E/AndroidRuntime(16936): at android.view.View.performClick(View.java:4475)
01-20 19:24:08.554: E/AndroidRuntime(16936): at android.view.View$PerformClick.run(View.java:18786)
01-20 19:24:08.554: E/AndroidRuntime(16936): at android.os.Handler.handleCallback(Handler.java:730)
01-20 19:24:08.554: E/AndroidRuntime(16936): at android.os.Handler.dispatchMessage(Handler.java:92)
01-20 19:24:08.554: E/AndroidRuntime(16936): at android.os.Looper.loop(Looper.java:176)
01-20 19:24:08.554: E/AndroidRuntime(16936): at android.app.ActivityThread.main(ActivityThread.java:5419)
01-20 19:24:08.554: E/AndroidRuntime(16936): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 19:24:08.554: E/AndroidRuntime(16936): at java.lang.reflect.Method.invoke(Method.java:525)
01-20 19:24:08.554: E/AndroidRuntime(16936): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
01-20 19:24:08.554: E/AndroidRuntime(16936): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
01-20 19:24:08.554: E/AndroidRuntime(16936): at dalvik.system.NativeStart.main(Native Method)
ItemDatabase.java
package com.example.fooditemmonitor;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
public final class ItemDatabase {
// To prevent someone from accidentally instantiating the contract class,
// give it an empty constructor.
public ItemDatabase() {
}
/* Inner class that defines the table contents */
public static abstract class Database implements BaseColumns {
public static final String TABLE_NAME = "foodItems";
public static final String COLUMN_NAME_ENTRY_ID = "entryid";
public static final String COLUMN_NAME_BARCODE = "barcode";
public static final String COLUMN_NAME_TITLE = "title";
public static final String COLUMN_NAME_QUANTITY = "quantity";
public static final String COLUMN_NAME_DATE = "date";
}
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ", ";
private static final String SQL_CREATE_ENTRIES = "CREATE TABLE "
+ Database.TABLE_NAME + " (" + Database._ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ Database.COLUMN_NAME_ENTRY_ID + TEXT_TYPE + COMMA_SEP
+ Database.COLUMN_NAME_DATE + TEXT_TYPE + COMMA_SEP
+ Database.COLUMN_NAME_BARCODE + TEXT_TYPE + COMMA_SEP
+ Database.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP
+ Database.COLUMN_NAME_QUANTITY + TEXT_TYPE + COMMA_SEP + " )";
private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS "
+ Database.TABLE_NAME;
public class ItemDatabaseHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database
// version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "ItemDatabase.db";
public ItemDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade
// policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
}
public static class ProductDatabase {
String title;
String barcode;
double quantity;
}
}
AddItem.java
package com.example.fooditemmonitor;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.example.fooditemmonitor.ItemDatabase.ProductDatabase;
public class AddItem extends Activity implements OnClickListener {
EditText mBarcodeEdit;
EditText mTitleEdit;
EditText mQuantityEdit;
private static final int REQUEST_BARCODE = 0;
private static final ProductDatabase mProductData = new ProductDatabase();
private final String PRODUCT_TABLE = "foodItems";
public static final String DATABASE_NAME = "ItemDatabase.db";
// a reference to the database used by this application/object
ItemDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_product);
db = new ItemDatabase();
final Button mScanButton = (Button) findViewById(R.id.scanButton);
mScanButton.setOnClickListener(this);
final Button mAddButton = (Button) findViewById(R.id.addButton);
mAddButton.setOnClickListener(this);
final Button currentInventory = (Button) findViewById(R.id.backToCurrent);
currentInventory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(AddItem.this, CurrentItems.class));
}
});
mBarcodeEdit = (EditText) findViewById(R.id.barcodeEdit);
mTitleEdit = (EditText) findViewById(R.id.titleEdit);
mQuantityEdit = (EditText) findViewById(R.id.quantityEdit);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.resetButton:
mBarcodeEdit.setText("");
mTitleEdit.setText("");
mQuantityEdit.setText("");
break;
case R.id.scanButton:
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, REQUEST_BARCODE);
break;
case R.id.addButton:
String barcode = mBarcodeEdit.getText().toString();
String title = mTitleEdit.getText().toString();
String quantity = mQuantityEdit.getText().toString();
SQLiteDatabase checkDB = null;
String errors = validateFields(barcode, title, quantity);
if (errors.length() > 0) {
showInfoDialog(this, "Please fix errors", errors);
}
else {
// Insert user information here
checkDB = SQLiteDatabase.openDatabase(DATABASE_NAME, null,
SQLiteDatabase.OPEN_READWRITE);
mProductData.barcode = barcode;
mProductData.title = title;
mProductData.quantity = Double.valueOf(quantity);
checkDB.execSQL("INSERT INTO " + PRODUCT_TABLE + " VALUES ("
+ getDateTime() + "," + mProductData + ")");
checkDB.close();
showInfoDialog(this, "Success", "Product saved successfully");
resetForm();
}
}
}
private String getDateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd",
Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
private void showInfoDialog(Context context, String title,
String information) {
new AlertDialog.Builder(context).setMessage(information)
.setTitle(title)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
private void resetForm() {
// TODO Auto-generated method stub
mBarcodeEdit.getText().clear();
mTitleEdit.getText().clear();
mQuantityEdit.getText().clear();
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_BARCODE) {
if (resultCode == RESULT_OK) {
String barcode = intent.getStringExtra("SCAN_RESULT");
mBarcodeEdit.setText(barcode);
} else if (resultCode == RESULT_CANCELED) {
finish();
}
}
}
private static String validateFields(String barcode, String title,
String quantity) {
StringBuilder errors = new StringBuilder();
if (barcode.matches("^\\s*$")) {
errors.append("Barcode required - please scan\n");
}
if (title.matches("^\\s*$")) {
errors.append("Please enter a title\n");
}
if (!quantity.matches("^-?\\d+(.\\d+)?$")) {
errors.append("Please enter a numeric quantity\n");
}
return errors.toString();
}
}
尝试过其他教程,但没有帮助! :/
所有帮助将不胜感激!
答案 0 :(得分:2)
这是错误代码=:http://www.sqlite.org/c3ref/c_abort.html。
Please check your db exists in /data/data/packagename/database folder. Its not created/present. Please use context.openOrCreateDatabase() to create file.
如果您的文件位于资产文件夹中,则其文件不可写。您需要复制到app的sdcard / database文件夹
答案 1 :(得分:0)
根据这个post你应该创建你的数据库,如果它不存在,你的错误说:
public PersonDbHelper(Context ctx) {
try {
db = ctx.openDatabase(DATABASE_NAME, null);
} catch (FileNotFoundException e) {
try {
db =
ctx.createDatabase(DATABASE_NAME, DATABASE_VERSION, 0,
null);
db.execSQL(DATABASE_CREATE);
} catch (FileNotFoundException e1) {
db = null;
}
}
}
很抱歉只需复制粘贴引用的代码,但我希望你能得到你需要做的事情。