Domanda

I create a Database in Android with SQLite. I create a little class that extract some datas from my DB. This is my class code:

package com.example.prenotazione_esame;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class GetDataDb {

    public String [] getAmb(SQLiteDatabase db){
        //"SELECT NOME_AMB FROM T_DIZ_AMB";
        String [] colonne = {"NOME_AMB"};
        Cursor cursor = db.query("T_DIZ_AMB", colonne, null, null, null, null, null);
        int numero_righe=cursor.getCount();

        //inizializzo le variabili necessarie. Con un ciclo for creo un array
        //di stringhe con i nomi degli ambulatori
        int k;
        String [] ambulatorio;
        ambulatorio = new String[numero_righe];
        for(k=0;k<numero_righe+1;k++){
            ambulatorio[k]=cursor.getString(k);
        }
        return ambulatorio;
    }
}

my Database code is :

package com.example.prenotazione_esame;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class LoginDataBase extends SQLiteOpenHelper {
    private static final String DB_NAME="Login_DB";
    private static final int DB_VERSION=1;

    public LoginDataBase(Context context){
        super(context,DB_NAME,null,DB_VERSION);
    }
    @Override 
     public void onCreate(SQLiteDatabase db) { 
        String sql="";
        sql+= "CREATE TABLE T_LOGIN (";
        sql+= " _id_LOGIN INTEGER PRIMARY KEY AUTOINCREMENT,";
        sql+= " USERNAME TEXT NOT NULL,";
        sql+= " PASSWORD TEXT NOT NULL";
        sql+=")";
        db.execSQL(sql);

        String sql2="";
        sql2+= "CREATE TABLE T_PROFILO (";
        sql2+= " _id_PROFILO INTEGER PRIMARY KEY AUTOINCREMENT,";
        sql2+= " NOME TEXT NOT NULL,";
        sql2+= " COGNOME TEXT NOT NULL,";
        sql2+= " ETA TEXT NOT NULL,";
        sql2+= " SESSO TEXT(1) NOT NULL,";
        sql2+= " CODICE_FISCALE TEXT NOT NULL,";
        sql2+= " CITTA TEXT NOT NULL,";
        sql2+= " INDIRIZZO TEXT NOT NULL,";
        sql2+= " TELEFONO TEXT NOT NULL,";
        sql2+= " _id_L INTEGER UNIQUE NOT NULL,";
        sql2+= " FOREIGN KEY(_id_L) REFERENCES T_LOGIN(_id_LOGIN)";
        sql2+= ")";
        db.execSQL(sql2);

        String sql3="";
        sql3+= "CREATE TABLE T_DIZ_AMB (";
        sql3+= " _id_AMB INTEGER PRIMARY KEY AUTOINCREMENT,";
        sql3+= " NOME_AMB TEXT NOT NULL,";
        sql3+= ")";
        db.execSQL(sql3);

        String sql4="";
        sql4+= "CREATE TABLE T_DIZ_PRE (";
        sql4+= " _id_PRE INTEGER PRIMARY KEY AUTOINCREMENT,";
        sql4+= " NOME_PRE TEXT NOT NULL,";
        sql4+= " _id_FK_AMB INTEGER NOT NULL,";
        sql4+= " FOREIGN KEY(_id_FK_AMB) REFERENCES T_DIZ_AMB(_id_AMB)";
        sql4+= ")";
        db.execSQL(sql4);

        String sql5="";
        sql5+="CREATE TABLE T_RIEPILOGO (";
        sql5+=" _id_RIEPILOGO INTEGER PRIMARY KEY AUTOINCREMENT,";
        sql5+= " DATA DATE NOT NULL,";
        sql5+= " ORA TIME NOT NULL,";
        sql5+= " _id_FK_L2 INTEGER NOT NULL,";
        sql5+= " FOREIGN KEY(_id_FK_L2) REFERENCES T_LOGIN(_id_LOGIN),";
        sql5+= " _id_FK_PRE INTEGER NOT NULL,";
        sql5+= " FOREIGN KEY(_id_FK_PRE) REFERENCES T_DIZ_PRE(_id_PRE)";
        sql5+= ")";
        db.execSQL(sql5);

        //INSERT di dati
        String sql6="";
        sql6+= "INSERT INTO T_DIZ_AMB (_id_AMB,NOME_AMB)";
        sql6+= "VALUES (1,'Cardiologia')";
        db.execSQL(sql6);
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
        // Aggiornamento delle tabelle 
     } 
}

Eclipse say that I haven't created T_DIZ_AMB table. But watching my Database Code I say that I have created it. This is my logcat:

05-15 05:00:31.567: E/AndroidRuntime(1109): FATAL EXCEPTION: main
05-15 05:00:31.567: E/AndroidRuntime(1109): Process: com.example.prenotazione_esame, PID: 1109
05-15 05:00:31.567: E/AndroidRuntime(1109): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prenotazione_esame/com.example.prenotazione_esame.Prenota}: android.database.sqlite.SQLiteException: no such table: T_DIZ_AMB (code 1): , while compiling: SELECT NOME_AMB FROM T_DIZ_AMB
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.os.Looper.loop(Looper.java:136)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at java.lang.reflect.Method.invokeNative(Native Method)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at java.lang.reflect.Method.invoke(Method.java:515)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at dalvik.system.NativeStart.main(Native Method)
05-15 05:00:31.567: E/AndroidRuntime(1109): Caused by: android.database.sqlite.SQLiteException: no such table: T_DIZ_AMB (code 1): , while compiling: SELECT NOME_AMB FROM T_DIZ_AMB
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at com.example.prenotazione_esame.GetDataDb.getAmb(GetDataDb.java:11)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at com.example.prenotazione_esame.Prenota.onCreate(Prenota.java:25)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.app.Activity.performCreate(Activity.java:5231)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-15 05:00:31.567: E/AndroidRuntime(1109):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-15 05:00:31.567: E/AndroidRuntime(1109):     ... 11 more
05-15 05:00:34.737: I/Process(1109): Sending signal. PID: 1109 SIG: 9

How Can I resolve this error? Thank you.

RE-EDIT: I remove the comma, but now I have this error:

05-15 04:59:49.777: D/dalvikvm(1083): Not late-enabling CheckJNI (already on)
05-15 04:59:55.067: D/(1083): HostConnection::get() New Host Connection established 0xb744aab8, tid 1083
05-15 04:59:55.217: W/EGL_emulation(1083): eglSurfaceAttrib not implemented
05-15 04:59:55.247: D/OpenGLRenderer(1083): Enabling debug mode 0
05-15 05:00:14.357: D/InputEventConsistencyVerifier(1083): KeyEvent: ACTION_UP but key was not down.
05-15 05:00:14.357: D/InputEventConsistencyVerifier(1083):   in android.widget.EditText{b2d8b5b8 VFED..CL .F...... 173,85-320,110 #7f05003c app:id/Password02}
05-15 05:00:14.357: D/InputEventConsistencyVerifier(1083):   0: sent at 3422921000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=3422921, downTime=3422776, deviceId=0, source=0x101 }
05-15 05:00:17.857: D/(1109): HostConnection::get() New Host Connection established 0xb7349238, tid 1109
05-15 05:00:18.057: W/EGL_emulation(1109): eglSurfaceAttrib not implemented
05-15 05:00:18.127: D/OpenGLRenderer(1109): Enabling debug mode 0
05-15 05:00:20.977: D/dalvikvm(1109): GC_FOR_ALLOC freed 55K, 5% free 2927K/3072K, paused 67ms, total 81ms
05-15 05:00:22.757: W/EGL_emulation(1109): eglSurfaceAttrib not implemented
05-15 05:00:22.867: I/Choreographer(1109): Skipped 63 frames!  The application may be doing too much work on its main thread.
05-15 05:00:29.057: W/EGL_emulation(1109): eglSurfaceAttrib not implemented
05-15 05:00:31.227: D/dalvikvm(1109): GC_FOR_ALLOC freed 83K, 5% free 3357K/3524K, paused 88ms, total 89ms
05-15 05:00:31.517: E/SQLiteLog(1109): (1) no such table: T_DIZ_AMB
05-15 05:00:31.527: D/AndroidRuntime(1109): Shutting down VM

This is my Prenota.java. As you can see this file call the class GetDataDb.

package com.example.prenotazione_esame;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.*;
import android.widget.Button;
import android.widget.ListView;
import android.view.View;
import android.widget.ArrayAdapter;


public class Prenota extends Activity {
    private LoginDataBase dbLogin;
    GetDataDb dbData = new GetDataDb();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.prenota);
        ListView lista = (ListView) findViewById(R.id.listViewAmbulatorio);
        dbLogin = new LoginDataBase(this);
        SQLiteDatabase db = dbLogin.getWritableDatabase();
        String [] array = dbData.getAmb(db);      
        //String [] array = {"a","b"};
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                this,R.layout.row,R.id.textViewList,array
                );
        lista.setAdapter(arrayAdapter);
        //arrayAdapter.add("A");
    }
}

This is my new logcat with the problem in the foreign key:

05-15 07:16:14.845: E/SQLiteLog(1493): (1) near "_id_FK_PRE": syntax error
05-15 07:16:14.855: D/AndroidRuntime(1493): Shutting down VM
05-15 07:16:14.855: W/dalvikvm(1493): threadid=1: thread exiting with uncaught exception (group=0xb2a5dba8)
05-15 07:16:14.895: E/AndroidRuntime(1493): FATAL EXCEPTION: main
05-15 07:16:14.895: E/AndroidRuntime(1493): Process: com.example.prenotazione_esame, PID: 1493
05-15 07:16:14.895: E/AndroidRuntime(1493): android.database.sqlite.SQLiteException: near "_id_FK_PRE": syntax error (code 1): , while compiling: CREATE TABLE T_RIEPILOGO ( _id_RIEPILOGO INTEGER PRIMARY KEY AUTOINCREMENT, DATA DATE NOT NULL, ORA TIME NOT NULL, _id_FK_L2 INTEGER NOT NULL, FOREIGN KEY(_id_FK_L2) REFERENCES T_LOGIN(_id_LOGIN), _id_FK_PRE INTEGER NOT NULL, FOREIGN KEY(_id_FK_PRE) REFERENCES T_DIZ_PRE(_id_PRE))
05-15 07:16:14.895: E/AndroidRuntime(1493):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at com.example.prenotazione_esame.LoginDataBase.onCreate(LoginDataBase.java:70)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at com.example.prenotazione_esame.LoginActivity.executeLogin(LoginActivity.java:54)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at com.example.prenotazione_esame.LoginActivity.access$1(LoginActivity.java:45)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at com.example.prenotazione_esame.LoginActivity$2.onClick(LoginActivity.java:36)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at android.view.View.performClick(View.java:4438)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at android.view.View$PerformClick.run(View.java:18422)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at android.os.Handler.handleCallback(Handler.java:733)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at android.os.Looper.loop(Looper.java:136)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at java.lang.reflect.Method.invokeNative(Native Method)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at java.lang.reflect.Method.invoke(Method.java:515)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-15 07:16:14.895: E/AndroidRuntime(1493):     at dalvik.system.NativeStart.main(Native Method)
05-15 07:16:17.375: I/Process(1493): Sending signal. PID: 1493 SIG: 9

new logcat after resolve foreign key:

05-15 07:31:38.235: E/AndroidRuntime(1592): FATAL EXCEPTION: main
05-15 07:31:38.235: E/AndroidRuntime(1592): Process: com.example.prenotazione_esame, PID: 1592
05-15 07:31:38.235: E/AndroidRuntime(1592): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prenotazione_esame/com.example.prenotazione_esame.Prenota}: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
05-15 07:31:38.235: E/AndroidRuntime(1592):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-15 07:31:38.235: E/AndroidRuntime(1592):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-15 07:31:38.235: E/AndroidRuntime(1592):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-15 07:31:38.235: E/AndroidRuntime(1592):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-15 07:31:38.235: E/AndroidRuntime(1592):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-15 07:31:38.235: E/AndroidRuntime(1592):     at android.os.Looper.loop(Looper.java:136)
05-15 07:31:38.235: E/AndroidRuntime(1592):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-15 07:31:38.235: E/AndroidRuntime(1592):     at java.lang.reflect.Method.invokeNative(Native Method)
05-15 07:31:38.235: E/AndroidRuntime(1592):     at java.lang.reflect.Method.invoke(Method.java:515)
05-15 07:31:38.235: E/AndroidRuntime(1592):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-15 07:31:38.235: E/AndroidRuntime(1592):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-15 07:31:38.235: E/AndroidRuntime(1592):     at dalvik.system.NativeStart.main(Native Method)
05-15 07:31:38.235: E/AndroidRuntime(1592): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
05-15 07:31:38.235: E/AndroidRuntime(1592):     at com.example.prenotazione_esame.GetDataDb.getAmb(GetDataDb.java:21)
05-15 07:31:38.235: E/AndroidRuntime(1592):     at com.example.prenotazione_esame.Prenota.onCreate(Prenota.java:26)
05-15 07:31:38.235: E/AndroidRuntime(1592):     at android.app.Activity.performCreate(Activity.java:5231)
05-15 07:31:38.235: E/AndroidRuntime(1592):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-15 07:31:38.235: E/AndroidRuntime(1592):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-15 07:31:38.235: E/AndroidRuntime(1592):     ... 11 more
È stato utile?

Soluzione 3

This is my new logcat with the problem in the foreign key:

Move the FOREIGN KEY declarations at the end. You cannot have column definitions like _id_FK_PRE INTEGER NOT NULL after a table constraint like FOREIGN KEY, as described in the syntax diagram:

enter image description here


ArrayIndexOutOfBoundsException: The condition here is wrong:

ambulatorio = new String[numero_righe];
for(k=0;k<numero_righe+1;k++){
    ambulatorio[k]=cursor.getString(k);
}

Change to k<numero_righe e.g. remove the +1.

Change getString(k) to getString(0) since there's only one column value in the cursor.

Also consider moving the cursor to a new row with moveToNext() unless you want the same value over and over again.

Altri suggerimenti

Remove the extra , in the line

sql3+= " NOME_AMB TEXT NOT NULL,";

and try again.

Your query is wrong

 String sql3="";
        sql3+= "CREATE TABLE T_DIZ_AMB (";
        sql3+= " _id_AMB INTEGER PRIMARY KEY AUTOINCREMENT,";
        sql3+= " NOME_AMB TEXT NOT NULL,"; //comma should not be here
        sql3+= ")";

Remove the comma "," from the last field and change it as below;

String sql3="";
        sql3+= "CREATE TABLE T_DIZ_AMB (";
        sql3+= " _id_AMB INTEGER PRIMARY KEY AUTOINCREMENT,";
        sql3+= " NOME_AMB TEXT NOT NULL";
        sql3+= ")";

EDITED:

Define all your FORIENG KEY at the last in your table that is the rule. Change your query as below:

    sql5+= " _id_FK_L2 INTEGER NOT NULL,";
    sql5+= " _id_FK_PRE INTEGER NOT NULL,";
    sql5+= " FOREIGN KEY(_id_FK_L2) REFERENCES T_LOGIN(_id_LOGIN),";
    sql5+= " FOREIGN KEY(_id_FK_PRE) REFERENCES T_DIZ_PRE(_id_PRE)";
    sql5+= ")";

Remove last comma(,) (NOME_AMB TEXT NOT NULL,) in this SQL statement:

String sql3="";
        sql3+= "CREATE TABLE T_DIZ_AMB (";
        sql3+= " _id_AMB INTEGER PRIMARY KEY AUTOINCREMENT,";
        sql3+= " NOME_AMB TEXT NOT NULL,";
        sql3+= ")";
        db.execSQL(sql3);

Check your logs while executing this query, you will get exception while executing this line...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top