2

我正在基于 SQLite 数据库在 Android 中构建应用程序。

不幸的是,它似乎不能很好地工作,因为它只能从大约 50 个文件中导入一个带有 sql 语句的文件。

结构是:

  • 我有一个包含多个元素的 ListView
  • 选择 ListView 中的项目后,启动一个新的 Activity (ShowPoints.java),并在带有 Bundle 对象的 Activity 之间传递一个额外的元素,这是一个 int 值。
  • 新活动 (ShowPoints) 包含一个表单,用户在该表单中编写内容并可以搜索数据库表。
  • 在 ShowPoints 中,我编写了一个开关,基于 int 值,将另一个 int 元素的值设置为我的 raw 文件夹中的一个 Raw 资源,其中填充了一些包含 SQL 语句的不带扩展名的文件。在每个文件中都有一个“CREATE TABLE ....;” 和一千行像“INSERT INTO.... VALUES....;”
  • 在 ShowPoints 中,我创建了一个新对象 MyDatabase,传递了创建新 DbHelper 对象的资源 int (R.raw.blahblah),我在其中创建了数据库并从文件中读取,然后逐行执行 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);

}

}

我的数据库.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');
.....

谢谢你的支持:)

4

1 回答 1

0

试试这个可能会帮助你:

我认为你应该把这个_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){}
于 2013-07-06T13:53:01.763 回答