lundi 31 août 2015

Own database in Android studio SQlite no such table

I am sorry but I read some topics about sqlite but I did not solve my problem. The software is very simply below the code. Please help me to found a solution:

The database and project file, if you want I can send all project by mail.

Below the code os SQLlite for Database creation

CREATE TABLE Items ( _ID INTEGER, LastName TEXT, FirstName TEXT, Address TEXT, City TEXT, PRIMARY KEY(_ID) );

CREATE TABLE android_metadata ( locale TEXT DEFAULT 'en_US' );

MAIN ACTIVITY

package med.myapplication;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        Context context = getApplicationContext();
        SQLiteAdapter miodb = new SQLiteAdapter(this);

        final TextView mioTextView1 = (TextView) findViewById(R.id.textView);

        miodb.openToRead();
        String contenutoletto = miodb.queueAll();
        miodb.close();
        mioTextView1.setText(contenutoletto);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

The SQLiteAdapter ---> The app blocks on cursor = sqLiteDatabase.query("Items",null,null,null,null,null,null);

package med.myapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class SQLiteAdapter  {
    public static String DB_PATH = "/data/data/med.myapplication/databases/";
    public static final String MYDATABASE_NAME = "NuovoDB.db";
    public static final String MYDATABASE_TABLE = "Items";
    public static final int MYDATABASE_VERSION = 1;
    public static final String KEY_CONTENT = "Colonna";


    private SQLiteHelper sqLiteHelper;
    private SQLiteDatabase sqLiteDatabase;

    private Context context;

    public SQLiteAdapter(Context c){
        context = c;
    }

    public SQLiteAdapter openToRead() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getReadableDatabase();
        return this;
    }

    public SQLiteAdapter openToWrite() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        return this;
    }

    public void close(){
        sqLiteHelper.close();
    }

    public long insert(String content){

        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_CONTENT, content);
        return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
    }

    public int deleteAll(){
        return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
    }

    public String queueAll(){
        //String[] columns = new String[]{KEY_CONTENT};
        String path = sqLiteDatabase.getPath();
        sqLiteDatabase.close();
        sqLiteDatabase = SQLiteDatabase.openDatabase(path,null,SQLiteDatabase.OPEN_READWRITE);
        Cursor cursor;

        long size = sqLiteDatabase.getMaximumSize();
        cursor = sqLiteDatabase.query("Items",null,null,null,null,null,null);

        String result = "";

        int index_CONTENT = cursor.getColumnIndex(KEY_CONTENT);
        for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){
            result = result + cursor.getString(index_CONTENT) + "\n";
        }

        return result;
    }

    public class SQLiteHelper extends SQLiteOpenHelper {

        public SQLiteHelper(Context context, String name,
                            SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            //db.execSQL(SCRIPT_CREATE_DATABASE);
            try {
                if (checkDataBase()) {
                }
                else
                {
                    copyDatabase();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }

        public void copyDatabase() throws IOException {
            if (checkDataBase()) {
            }else{
                InputStream assetsDB = context.getAssets().open("NuovoDB.db");
                OutputStream dbOut = new FileOutputStream(DB_PATH + "Items");
                byte[] buffer = new byte[1024];
                int length;
                while ((length = assetsDB.read(buffer))>0){
                    dbOut.write(buffer, 0, length);
                }
                dbOut.flush();
                dbOut.close();
                assetsDB.close();
            }
        }

        public boolean checkDataBase(){
            SQLiteDatabase checkDB = null;
            try{
                String myPath = DB_PATH + MYDATABASE_NAME;
                checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
            }catch(SQLiteException e){
                //database does't exist yet.
            }
            if(checkDB != null){
                checkDB.close();
            }
            if (checkDB != null ? true : false) return true;
            else return false;
        }

    }
}

Below the LOGCAT

E/SQLiteLog﹕ (1) no such table: E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{med.myapplication/med.myapplication.MainActivity}: android.database.sqlite.SQLiteException: no such table: Items (code 1): , while compiling: SELECT * FROM Items at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117) at android.app.ActivityThread.access$700(ActivityThread.java:134) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4867) 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:1007) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) at dalvik.system.NativeStart.main(Native Method) Caused by: android.database.sqlite.SQLiteException: no such table: Items (code 1): , while compiling: SELECT * FROM Items at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1013) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:624) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:37) at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161) at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032) at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200) at med.myapplication.SQLiteAdapter.queueAll(SQLiteAdapter.java:70) at med.myapplication.MainActivity.onCreate(MainActivity.java:23) at android.app.Activity.performCreate(Activity.java:5047) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)             at android.app.ActivityThread.access$700(ActivityThread.java:134)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)             at android.os.Handler.dispatchMessage(Handler.java:99)             at android.os.Looper.loop(Looper.java:137)             at android.app.ActivityThread.main(ActivityThread.java:4867)             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:1007)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)             at dalvik.system.NativeStart.main(Native Method)

Aucun commentaire:

Enregistrer un commentaire