-1

I am trying copy db file to assets folder, the Toats notificated: copy success, but when I implement display table info, "Android SQLiteException no such table" error was displayed. Please help me. Thank you so much.

Main: package 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());
    }
}
}

This is my Contact Class: package model; /*

*/

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;
}
}    
ɢʀᴜɴᴛ
  • 32,025
  • 15
  • 116
  • 110
vaart12345
  • 51
  • 1
  • 4

1 Answers1

0

You never create the table, which you should understand from the exception considering it contains "No such table". In the code, you never create the database, you only try to read from it. The solution is rather simple: Add an SQL query after you open/create the database:

CREATE TABLE IF NOT EXISTS Contacts (Rows here)
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Thanks for your answer, but let me explain more detail. In my db: dbContact.sqlite, existing the Contact table. – vaart12345 Jul 28 '17 at 11:47
  • Thanks for your answer, but let me explain more detail. In my db: dbContact.sqlite, existing the Contact table. So i copyed to Assets folder, and use the code to copy this database to my phone. At this step, Toast notificated: copy success, after that, when implement hienThiToanBoSP(); method to display db on device, the app was crash. I am sorry for my english, and i am a newbie. Please let me now the right code, and where to add? Thanks & best regards. – vaart12345 Jul 28 '17 at 12:02
  • because in dbContact.sqlite is existing Contact table, so i think do not need CREATE TABLE. – vaart12345 Jul 28 '17 at 12:08
  • I don't are what you **think**.The table doesn't exist in the database. You wouldn't get the exception if it existed. Be aware that it is case sensitive – Zoe Jul 28 '17 at 13:44
  • Thanks for your reply, but this is my db: [Link](https://drive.google.com/open?id=0BwEu0sNIKc_fVXBaT1ItOTQxYnc). When i create this db, i also create the Contact table on it. I am coding follow the video on my course. And the teacher' s code is worked, the data was displayed on the emulator. When i test, i use my phone. Thanks & regards. – vaart12345 Jul 29 '17 at 14:08