Java SQLite android不会创建所有表
我正在Android中基于SQLite数据库构建一个应用程序 不幸的是,它似乎不能很好地工作,因为它能够在大约50个文件中只导入一个带有sql语句的文件 结构如下:Java SQLite android不会创建所有表,java,android,sql,sqlite,Java,Android,Sql,Sqlite,我正在Android中基于SQLite数据库构建一个应用程序 不幸的是,它似乎不能很好地工作,因为它能够在大约50个文件中只导入一个带有sql语句的文件 结构如下: 我有一个包含多个元素的列表视图 一旦从ListView中选择了一个项目,启动一个新的活动(ShowPoints.java),并在活动之间通过一个Bundle对象传递一个额外的元素,这是一个int值 新的活动(ShowPoints)由一个表单组成,在该表单中,用户可以编写内容并搜索数据库的表 在ShowPoints中,我编写了一个开
- 我有一个包含多个元素的列表视图
- 一旦从ListView中选择了一个项目,启动一个新的活动(ShowPoints.java),并在活动之间通过一个Bundle对象传递一个额外的元素,这是一个int值
- 新的活动(ShowPoints)由一个表单组成,在该表单中,用户可以编写内容并搜索数据库的表
- 在ShowPoints中,我编写了一个开关,该开关基于int值,将另一个int元素的值设置为我的Raw文件夹中的一个Raw资源,其中填充了一些不带扩展名的文件,这些文件包含SQL语句。在每个文件中都有一个“createtable…”和上千行,如“insertinto….VALUES…”
- 在ShowPoints中,我创建了一个新对象MyDatabase,该对象传递资源int(R.raw.blahblah),该资源int创建了一个新的DbHelper对象,在该对象中我创建了数据库并从文件中读取数据,然后逐行执行SQL读取
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);
}
}
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"
+ "');";
*/
}
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
}
}
MyDatabase.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);
}
}
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"
+ "');";
*/
}
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(工作文件)
u 4x200(其中一个不工作文件)
感谢您的支持:)试试这可能会帮助您: 我认为您应该将这个\u db.execSQL(行)和_db.execSQL(s)代码放在try-catch块中 在DbHelper.java中 改变
while (( line = buffreader.readLine()) != null) {
//System.out.println(line);
_db.execSQL(line);
}
到
及
_db.execSQL(s);
到
感谢您的回答,我发现这是一个数据库不刷新的问题!所以问题是我没有在MyDatabase.java中编写onUpgrade方法,起初单击列表视图中的一个项目都很好,但是返回屏幕并单击另一个项目,应用程序试图创建另一个同名数据库,所以崩溃了!现在,我暂时使用的解决方案是,每次有人单击时,我都会创建一个新的数据库,然后我会正确地编写onupgrade方法!谢谢:)好的:)很好!但如果应用程序试图创建同名数据库,则不应崩溃,也就是说,它应该处理异常而不是崩溃!抱歉,我说得不好,它确实抛出了异常:)
try{ _db.execSQL(s);}
catch(Exception e){}