我正在尝试将db文件复制到assets文件夹,Toats通知:复制成功,但是当我实现显示表信息时," Android SQLiteException没有这样的表"显示错误。 请帮我。非常感谢你。
主: 包com.example.huyvu.home_sqlite;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import model.Contact;
public class MainActivity extends AppCompatActivity {
String DATABASE_NAME = "dbContact.sqlite";
String DB_PATH_SUFFIX = "/databases/";
SQLiteDatabase database = null;
ListView lvContact;
ArrayAdapter<Contact> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
processCopy();
addControls();
hienThiToanBoSP();
}
private void hienThiToanBoSP() {
database = openOrCreateDatabase(DATABASE_NAME,MODE_PRIVATE,null);
Cursor cursor = database.rawQuery("select * from Contact",null);
adapter.clear();
while (cursor.moveToNext()){
int ma = cursor.getInt(0);
String ten = cursor.getString(1);
String phone = cursor.getString(2);
Contact contact = new Contact(ma,ten,phone);
adapter.add(contact);
}
cursor.close();
}
private void addControls() {
lvContact = (ListView) findViewById(R.id.lvContact);
adapter = new ArrayAdapter<Contact>(MainActivity.this,
android.R.layout.simple_list_item_1);
lvContact.setAdapter(adapter);
}
private void processCopy(){
try {
File dbFile = getDatabasePath(DATABASE_NAME);
if(!dbFile.exists()){
copyDatabaseFromAsset();
Toast.makeText(MainActivity.this,"Copy DB to device success",Toast.LENGTH_SHORT).show();
}
}
catch (Exception ex){
Toast.makeText(MainActivity.this,ex.toString(),Toast.LENGTH_SHORT).show();
Log.e("Error",ex.toString());
}
}
private String getDatabasePath(){
return getApplicationInfo().dataDir+DB_PATH_SUFFIX+DATABASE_NAME;
}
private void copyDatabaseFromAsset() {
try {
InputStream myInput = getAssets().open(DATABASE_NAME);
String outFileName = getDatabasePath();
File f = new File(getApplicationInfo().dataDir+DB_PATH_SUFFIX);
if(!f.exists())
f.mkdir();//create named databases folder
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length=myInput.read(buffer))>0){
myOutput.write(length);
myOutput.flush();
myOutput.close();
myInput.close();
}
}
catch (Exception ex){
Log.e("Error",ex.toString());
}
}
}
这是我的联系班级: 包装模型; / *
* /
public class Contact {
private int ma;
private String ten;
private String phone;
public Contact() {
}
public Contact(int ma, String ten, String phone) {
this.ma = ma;
this.ten = ten;
this.phone = phone;
}
public int getMa() {
return ma;
}
public void setMa(int ma) {
this.ma = ma;
}
public String getTen() {
return ten;
}
public void setTen(String ten) {
this.ten = ten;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String toString(){
return this.ma + "-" + this.ten + "\n" + this.phone;
}
}
答案 0 :(得分:0)
你永远不会创建表,你应该从异常中理解它,因为它包含“没有这样的表”。在代码中,您永远不会创建数据库,您只能尝试从中读取数据。解决方案非常简单:打开/创建数据库后添加SQL查询:
CREATE TABLE IF NOT EXISTS Contacts (Rows here)