在我的Android应用程序中,我使用sqlite浏览器创建了一个数据库,并将其存入资产文件夹。然后我创建了一个名为EMPerson的表。 当用户单击“保存”按钮时,我需要添加用户定义的数据(用户可以将详细信息,如personName,date_of_birth,age,gender& bloodGrp)插入此EMPerson表中。
我的DBHelper类是这样的。
package my.easymedi.db;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import my.easymedi.entity.Person;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class DBHelper extends SQLiteOpenHelper {
/*Database attributes - the Android's default system path for your application database*/
private static final String pkg = "my.easymedi.controller";
private static String DB_PATH = "/data/data/" + pkg + "/databases/";
private static String DB_NAME = "EasyMediInfo.jpeg";
private static final int DB_VERSION = 1;
private final Context myContext;
private SQLiteDatabase myDatabase;
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
}
public void createDataBase() {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Toast.makeText(myContext, e.getMessage(), Toast.LENGTH_SHORT).show();
Log.d("CREATE_DB", e.getMessage());
}
}
}
private void copyDataBase() throws IOException {
InputStream databaseInput = null;
String outFileName = DB_PATH + DB_NAME; //path to copy the database
OutputStream databaseOutput = new FileOutputStream(outFileName); //open the empty db as an output stream
databaseInput = myContext.getAssets().open(DB_NAME);
byte[] buffer = new byte[512];
int length = databaseInput.read(buffer);
while(length > 0){
databaseOutput.write(buffer, 0, length);
databaseOutput.flush();
}
databaseOutput.flush();
databaseInput.close();
databaseOutput.close();
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
} catch (SQLiteException e) {
Toast.makeText(myContext, e.getMessage(), Toast.LENGTH_SHORT).show();
Log.d("Check_DB", e.getMessage());
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
public void openDatabase() {
String myPath = DB_PATH + DB_NAME;
myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
}
public void closeDatabase() {
if(myDatabase != null){
myDatabase.close();
}
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public boolean insertIntoDatabase(Person person) {
String query = "INSERT INTO EMPerson (COLUMN_PERSON_NAME, COLUMN_DOB , COLUMN_AGE, COLUMN_GENDER, COLUMN_BLOODGRP) "
+ " VALUES ('" + person.getName() + "', '" + person.getDate_of_birth() + "', '" + person.getAge() + "', '" + person.getGender() + "', '" + person.getBloodGrp() + "')";
try {
myDatabase.execSQL(query);
return true;
} catch (Exception e) {
return false;
}
}
}
这是我的AddNewPerson类片段。
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnSave:
personName = etName.getText().toString();
date_of_birth = tvDOB.getText().toString();
age = tvAge.getText().toString();
int selected_rb_ID = genderGrp.getCheckedRadioButtonId();
RadioButton rb = (RadioButton) findViewById(selected_rb_ID);
gender = rb.getText().toString();
bloodGrp = spiBloodGrp.getSelectedItem().toString();
Person person = new Person();
person.setName(personName);
person.setDate_of_birth(date_of_birth);
person.setAge(age);
person.setGender(gender);
person.setBloodGrp(bloodGrp);
//personArrayList.add(person);
DBHelper dbHelper = new DBHelper(this);
dbHelper.createDataBase();
dbHelper.openDatabase();
//SQLiteDatabase sqliteDatabase = dbHelper.getWritableDatabase();
boolean flag = dbHelper.insertIntoDatabase(person);
//long affectedColumnId = sqliteDatabase.insert(AndroidOpenDbHelper.TABLE_NAME_GPA, null, contentValues);
if(flag){
Toast.makeText(getApplicationContext(), "Values inserted", Toast.LENGTH_SHORT).show();
}
dbHelper.closeDatabase();
break;
当我运行应用程序日志时,cat说'没有这样的表:EMPerson'
任何人都可以解释这些代码的错误。
提前完成
答案 0 :(得分:0)
将 DB_NAME更改为资产中的EasyMediInfo.db
B / coz数据库文件的扩展名为 .db
私有静态字符串DB_NAME =“EasyMediInfo.db”;
编辑: -
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
if (android.os.Build.VERSION.SDK_INT >= 4.2) {
DB_PATH = myContext.getApplicationInfo().dataDir
+ "/databases/";
} else {
DB_PATH = "/data/data/" + myContext.getPackageName()
+ "/databases/";
}
}
答案 1 :(得分:0)
似乎有两个问题:
将数据库的扩展名从“.jpeg”更改为“.db”(只是对良好编码习惯的建议)
您的代码中似乎没有正确初始化“mydatabase”变量:
该行:
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
应该调用this.openDatabase(...)
同样,当您的数据库不存在时,您复制数据库但不再次初始化mydatabase(您应该再次调用openDatabase)
此外,您可以将openDatabase函数修改为
public boolean openDatabase() {
String myPath = DB_PATH + DB_NAME;
myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
return myDatabase!= null ? true : false;
}