Autore Topic: gestione database presistente  (Letto 465 volte)

Offline elehcim96

  • Nuovo arrivato
  • *
  • Post: 1
  • Respect: 0
    • Mostra profilo
gestione database presistente
« il: 25 Novembre 2012, 12:42:24 CET »
0
ciao ragazzi,
ho bisogno di un vostro importantissimo aiuto!
sto sviluppando un'applicazione che debba leggere tutti i dati di un database da me già creato con sqlitebrowser e inserirli in una spinner!
ho provato con questo codice ma ottengo continuamente errori... il logcat dice che non esiste la tabella da me cercata!
questo è il codice della classe SQLiteAdapter

Codice (Java): [Seleziona]
package com.example.chometro;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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 android.database.sqlite.SQLiteDatabase.CursorFactory;

public class SQLiteAdapter  {
 public static String DB_PATH = "/data/data/com.example.chometro/databases/";
 public static final String MYDATABASE_NAME = "inran.db";
 public static final String MYDATABASE_TABLE = "Alimenti";
 public static final int MYDATABASE_VERSION = 1;
 public static final String KEY_CONTENT = "Nome";

 public void copyDatabase() throws IOException {
            InputStream assetsDB = context.getAssets().open("inran.db");
            OutputStream dbOut = new FileOutputStream("/data/data/com.example.chometro/databases/inran.db");
         
            byte[] buffer = new byte[1024];
            int length;
            while ((length = assetsDB.read(buffer))>0){
              dbOut.write(buffer, 0, length);
            }
         
            dbOut.flush();
            dbOut.close();
            assetsDB.close();
        }
 
 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};
  Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
    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,
    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 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();
        }
        return checkDB != null ? true : false;
  }
 
 }
 
}

mentre questo è il codice della classe Main

Codice (Java): [Seleziona]
package com.example.chometro;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;



public class Main extends Activity {

        Spinner sp1, sp2, sp3, sp4, sp5, sp6, sp7, sp8;
        EditText txt1, txt2, txt3, txt4, txt5, txt6, txt7, txt8;
        Button v1, v2, v3, v4, v5, v6, v7, v8, x1, x2, x3, x4, x5, x6, x7, x8, pros;
        TextView lbl1, lbl2;
        private SQLiteAdapter miodb;
       
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
               
                sp1 = (Spinner) findViewById (R.id.spinner1);
                sp2 = (Spinner) findViewById (R.id.Spinner01);
                sp3 = (Spinner) findViewById (R.id.Spinner02);
                sp4 = (Spinner) findViewById (R.id.Spinner03);
                sp5 = (Spinner) findViewById (R.id.Spinner04);
                sp6 = (Spinner) findViewById (R.id.Spinner05);
                sp7 = (Spinner) findViewById (R.id.Spinner06);
                sp8 = (Spinner) findViewById (R.id.Spinner07);
                txt1 = (EditText) findViewById (R.id.editText1);
                txt2 = (EditText) findViewById (R.id.EditText01);
                txt3 = (EditText) findViewById (R.id.EditText02);
                txt4 = (EditText) findViewById (R.id.EditText03);
                txt5 = (EditText) findViewById (R.id.EditText04);
                txt6 = (EditText) findViewById (R.id.EditText05);
                txt7 = (EditText) findViewById (R.id.EditText06);
                txt8 = (EditText) findViewById (R.id.EditText07);
                v1 = (Button) findViewById (R.id.button1);
                v2 = (Button) findViewById (R.id.Button01);
                v3 = (Button) findViewById (R.id.Button02);
                v4 = (Button) findViewById (R.id.Button03);
                v5 = (Button) findViewById (R.id.Button04);
                v6 = (Button) findViewById (R.id.Button05);
                v7 = (Button) findViewById (R.id.Button06);
                v8 = (Button) findViewById (R.id.Button07);
                x1 = (Button) findViewById (R.id.Button08);
                x2 = (Button) findViewById (R.id.Button09);
                x3 = (Button) findViewById (R.id.Button10);
                x4 = (Button) findViewById (R.id.Button11);
                x5 = (Button) findViewById (R.id.Button12);
                x6 = (Button) findViewById (R.id.Button13);
                x7 = (Button) findViewById (R.id.Button14);
                x8 = (Button) findViewById (R.id.Button15);
                pros = (Button) findViewById (R.id.button2);
                lbl1 = (TextView) findViewById (R.id.textView1);
                lbl2 = (TextView) findViewById (R.id.textView2);
                miodb = new SQLiteAdapter (this);
               
                miodb.openToRead();
                String contenutoletto = miodb.queueAll();
                miodb.close();
                lbl1.setText(contenutoletto);
    }
}

il database si chiama inran.db ed è formato da una tabella Alimenti contenente le colonne "ID", "Nome" e "CHO"

cosa sbaglio?
grazie in anticipo ragazzi!

Offline Sakazaki

  • Utente normale
  • ***
  • Post: 396
  • Respect: +74
    • Mostra profilo
  • Dispositivo Android:
    Sony xperia Z
  • Play Store ID:
    Saka Labs
  • Sistema operativo:
    Windows 8
R: gestione database presistente
« Risposta #1 il: 25 Novembre 2012, 17:36:49 CET »
0
Il logcat con l'errore sarebbe utile.