簡體   English   中英

SQLite android不會創建所有表

[英]SQLite android doesnt create all tables

我正在基於SQLite數據庫在Android中構建應用程序。

不幸的是,它似乎不能很好地工作,因為它只能從大約50個文件中導入一個帶有sql語句的文件。

結構為:

  • 我有一個包含多個元素的ListView
  • 從ListView中選擇一個項目后,啟動一個新的Activity(ShowPoints.java),並在帶有Bundle Object的Activity之間傳遞一個額外的元素,即int值。
  • 新活動(ShowPoints)由一種表單組成,用戶可以在其中寫一些東西並可以搜索數據庫表。
  • 在ShowPoints中,我編寫了一個基於int值的開關,將另一個int元素的值設置為我的raw文件夾中的Raw資源之一,其中填充了一些不帶擴展名的包含SQL語句的文件。 在每個文件中都有一個“ CREATE TABLE ....;”。 一千行,例如“ INSERT INTO .... VALUES ....;”
  • 在ShowPoints中,我創建一個新對象MyDatabase,該對象傳遞資源int(R.raw.blahblah),該資源創建一個新的DbHelper對象,在其中創建數據庫,並從文件中讀取,然后逐行執行SQL讀取。

它僅適用於一個文件u_4x100,並且應用程序崩潰時提示沒有數據庫。

這是代碼:

ShowPoints.java

package it.gorlux.onyourmark;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ShowPoints extends Activity {

    EditText searchText;
    Cursor cursor;
    ListAdapter adapter;
    MyDatabase db;
    ListView peopleLv;

    DbHelper mDbHelper;
    SQLiteDatabase _db;     

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_points);

    Bundle b = getIntent().getExtras();
    int item_int = b.getInt("item");

    peopleLv=(ListView)findViewById(R.id.peopleLv);
    searchText = (EditText)findViewById(R.id.searchText);

    Context ctx = this;
    db=new MyDatabase(getApplicationContext(),item_int,this);

}

public void search(View view) {
    db.open();  //apriamo il db

    // || is the concatenation operation in SQLite
    adapter = new SimpleCursorAdapter(
            this, 
            R.layout.person, 
            db.searchLabel(searchText.getText().toString()), 
            new String[]{MyDatabase.PersonMetaData.PERSON_NAME_KEY,MyDatabase.PersonMetaData.PERSON_AGE_KEY},//queste colonne
            new int[]{R.id.nameTv,R.id.ageTv});
    peopleLv.setAdapter(adapter);

}

}

MyDatabase.java

package it.gorlux.onyourmark;

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

public class MyDatabase {

    SQLiteDatabase mDb;
    Context mContext;

    DbHelper mDbHelper;

    public int res;
    public String tabella = null;

    private static final String DB_NAME="punteggi";//nome del database<!--more-->
    private static final int DB_VERSION=1; //numero di versione del nostro database

    Context ctx2;

    public MyDatabase(Context ctx, int item, Context ctx2) {

        mContext=ctx;
        this.ctx2 = ctx2;


        int item2 = item;

        switch(item) {
            case 0: {res=R.raw.d_4x100;break;}case 1: {res=R.raw.d_4x200;break;}case 2: {res=R.raw.d_4x400;break;}case 3: {res=R.raw.d_4x800;break;}case 4: {res=R.raw.d_4x1500;break;}case 5: {res=R.raw.d_100h;break;}case 6: {res=R.raw.d_100m;break;}case 7: {res=R.raw.d_200h;break;}case 8: {res=R.raw.d_200m;break;}case 9: {res=R.raw.d_400h;break;}case 10: {res=R.raw.d_400m;break;}case 11: {res=R.raw.d_800m;break;}case 12: {res=R.raw.d_1500m;break;}case 13: {res=R.raw.d_3000m;break;}case 14: {res=R.raw.d_3000siepi;break;}case 15: {res=R.raw.d_5000m;break;}case 16: {res=R.raw.d_10000m;break;}case 17: {res=R.raw.d_alto;break;}case 18: {res=R.raw.d_asta;break;}case 19: {res=R.raw.d_disco;break;}case 20: {res=R.raw.d_giavellotto;break;}case 21: {res=R.raw.d_lungo;break;}case 22: {res=R.raw.d_marcia5km;break;}case 23: {res=R.raw.d_martello;break;}case 24: {res=R.raw.d_peso;break;}case 25: {res=R.raw.d_svedese;break;}case 26: {res=R.raw.d_triplo;break;}case 27: {res=R.raw.u_4x100;break;}case 28: {res=R.raw.u_4x200;break;}case 29: {res=R.raw.u_4x400;break;}case 30: {res=R.raw.u_4x800;break;}case 31: {res=R.raw.u_4x1500;break;}case 32: {res=R.raw.u_100m;break;}case 33: {res=R.raw.u_110h;break;}case 34: {res=R.raw.u_200h;break;}case 35: {res=R.raw.u_200m;break;}case 36: {res=R.raw.u_400h;break;}case 37: {res=R.raw.u_400m;break;}case 38: {res=R.raw.u_800m;break;}case 39: {res=R.raw.u_1500m;break;}case 40: {res=R.raw.u_3000m;break;}case 41: {res=R.raw.u_3000siepi;break;}case 42: {res=R.raw.u_5000m;break;}case 43: {res=R.raw.u_10000m;break;}case 44: {res=R.raw.u_alto;break;}case 45: {res=R.raw.u_asta;break;}case 46: {res=R.raw.u_disco;break;}case 47: {res=R.raw.u_giavellotto;break;}case 48: {res=R.raw.u_lungo;break;}case 49: {res=R.raw.u_marcia10km;break;}case 50: {res=R.raw.u_martello;break;}case 51: {res=R.raw.u_peso;break;}case 52: {res=R.raw.u_svedese;break;}case 53: {res=R.raw.u_triplo;break;}       
            default : {break;}
        }   

        switch(item2) {
            case 0:{tabella="d_4x100";break;}case 1:{tabella="d_4x200";break;}case 2:{tabella="d_4x400";break;}case 3:{tabella="d_4x800";break;}case 4:{tabella="d_4x1500";break;}case 5:{tabella="d_100h";break;}case 6:{tabella="d_100m";break;}case 7:{tabella="d_200h";break;}case 8:{tabella="d_200m";break;}case 9:{tabella="d_400h";break;}case 10:{tabella="d_400m";break;}case 11:{tabella="d_800m";break;}case 12:{tabella="d_1500m";break;}case 13:{tabella="d_3000m";break;}case 14:{tabella="d_3000siepi";break;}case 15:{tabella="d_5000m";break;}case 16:{tabella="d_10000m";break;}case 17:{tabella="d_alto";break;}case 18:{tabella="d_asta";break;}case 19:{tabella="d_disco";break;}case 20:{tabella="d_giavellotto";break;}case 21:{tabella="d_lungo";break;}case 22:{tabella="d_marcia5km";break;}case 23:{tabella="d_martello";break;}case 24:{tabella="d_peso";break;}case 25:{tabella="d_svedese";break;}case 26:{tabella="d_triplo";break;}case 27:{tabella="u_4x100";break;}case 28:{tabella="u_4x200";break;}case 29:{tabella="u_4x400";break;}case 30:{tabella="u_4x800";break;}case 31:{tabella="u_4x1500";break;}case 32:{tabella="u_100m";break;}case 33:{tabella="u_110h";break;}case 34:{tabella="u_200h";break;}case 35:{tabella="u_200m";break;}case 36:{tabella="u_400h";break;}case 37:{tabella="u_400m";break;}case 38:{tabella="u_800m";break;}case 39:{tabella="u_1500m";break;}case 40:{tabella="u_3000m";break;}case 41:{tabella="u_3000siepi";break;}case 42:{tabella="u_5000m";break;}case 43:{tabella="u_10000m";break;}case 44:{tabella="u_alto";break;}case 45:{tabella="u_asta";break;}case 46:{tabella="u_disco";break;}case 47:{tabella="u_giavellotto";break;}case 48:{tabella="u_lungo";break;}case 49:{tabella="u_marcia10km";break;}case 50:{tabella="u_martello";break;}case 51:{tabella="u_peso";break;}case 52:{tabella="u_svedese";break;}case 53:{tabella="u_triplo";break;}
            default:{break;}
        }

        mDbHelper=new DbHelper(ctx2, DB_NAME, null, DB_VERSION, res);

    }

    public void open() {  //apriamo il database, rendendolo scrivibile e leggibile

        mDb=mDbHelper.getWritableDatabase();

    }

    public void close() { //chiudiamo il database

        mDb.close();

    }

// Vediamo ora come aggiungere tabelle e campi alle tabelle del db

/*  public void insertPerson(String name,String punt) { //metodo per inserire i dati

        ContentValues cv=new ContentValues();
        cv.put(PersonMetaData.PERSON_NAME_KEY, name);
        cv.put(PersonMetaData.PERSON_AGE_KEY, punt);
        mDb.insert(PersonMetaData.PERSON_TABLE, null, cv);

    }*/

/*  public Cursor fetchProducts() { //metodo per fare la query di tutti i dati

        return mDb.query(PersonMetaData.PERSON_TABLE, null,null,null,null,null,null);

    }*/

    public Cursor searchLabel(String s) {
        return mDb.rawQuery("SELECT _id, prestazione, punteggio FROM " + tabella + " WHERE prestazione || ' ' || punteggio LIKE ?", 
                new String[]{"%" + s + "%"});
    }

static class PersonMetaData {  // i metadati della tabella, accessibili ovunque

/*  static final String PERSON_TABLE = tabella;
*/  static final String ID = "_id";
    static final String PERSON_NAME_KEY = "prestazione";
    static final String PERSON_AGE_KEY = "punteggio";

}

/*public static final String PERSON_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "  //codice sql di creazione della tabella

+ tabella + " ("
+ PersonMetaData.ID+ " integer primary key autoincrement, "
+ PersonMetaData.PERSON_NAME_KEY + " text not null, "
+ PersonMetaData.PERSON_AGE_KEY + " text not null"
+ "');";
*/




}

DbHelper.java

package it.gorlux.onyourmark;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHelper extends SQLiteOpenHelper { //classe che ci aiuta nella creazione del db

    int item;
    Context ctx;

    public DbHelper(Context context, String name, CursorFactory factory,int version, int item) {

        super(context, name, factory, version);
        this.item = item;
        this.ctx = context;

    }

@Override

    public void onCreate(SQLiteDatabase _db) { //solo quando il db viene creato, creiamo la tabella

    InputStream inputStream = ctx.getResources().openRawResource( item );
    InputStreamReader inputreader = new InputStreamReader(inputStream);
    BufferedReader buffreader = new BufferedReader(inputreader);
    String line;

    try {
        while (( line = buffreader.readLine()) != null) {
            //System.out.println(line);
            _db.execSQL(line);
        }
    } catch (IOException e) {
    }

/*      _db.execSQL(MyDatabase.PERSON_TABLE_CREATE);

        String[] queries = MyDatabase.exec.split(";");
        for(String query : queries){
            _db.execSQL(query);
        }

        String[] queries2 = MyDatabase.exec2.split(";");
        for(String query : queries2){
            _db.execSQL(query);
        }*/

    }

public void execute(SQLiteDatabase _db, String s) {
    _db.execSQL(s);
}

@Override

    public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {

    //qui mettiamo eventuali modifiche al db, se nella nostra nuova versione della app, il db cambia numero di versione

    }

}

u_4x100(工作文件)

CREATE TABLE IF NOT EXISTS u_4x100 (_id integer primary key autoincrement, prestazione text, punteggio text);
INSERT INTO u_4x100 (_id, prestazione, punteggio) VALUES (1, '37.40', '1300');
INSERT INTO u_4x100 (_id, prestazione, punteggio) VALUES (2, '38.10', '1207');
INSERT INTO u_4x100 (_id, prestazione, punteggio) VALUES (3, '38.80', '1129');
INSERT INTO u_4x100 (_id, prestazione, punteggio) VALUES (4, '39.59', '1059');
.....

u_4x200(其中一個無效文件)

CREATE TABLE IF NOT EXISTS u_4x200 (_id integer primary key autoincrement, prestazione text, punteggio text);
INSERT INTO u_4x200 (_id, prestazione, punteggio) VALUES (1, '1:18.68', '1300');
INSERT INTO u_4x200 (_id, prestazione, punteggio) VALUES (2, '1:19.53', '1230');
INSERT INTO u_4x200 (_id, prestazione, punteggio) VALUES (3, '1:20.51', '1160');
INSERT INTO u_4x200 (_id, prestazione, punteggio) VALUES (4, '1:21.72', '1090');
INSERT INTO u_4x200 (_id, prestazione, punteggio) VALUES (5, '1:23.41', '1020');
.....

謝謝你的支持:)

試試這個可能對您有幫助:

我認為您應該將此_db.execSQL(line)和_db.execSQL(s)代碼放入try catch塊中;

在DbHelper.java中

更改

      while (( line = buffreader.readLine()) != null) {
        //System.out.println(line);
        _db.execSQL(line);
    }

   while (( line = buffreader.readLine()) != null) {
        //System.out.println(line);
        try 
        {
        _db.execSQL(line);
        }
        catch(Exception e)
        {}
    }

       _db.execSQL(s);

       try{  _db.execSQL(s);}
       catch(Exception e){}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM