SQLite 지원으로 응용 프로그램을 만들려고합니다. 그러나 내가 응용 프로그램을 시작하면 그냥 멈추고 강제로 종료해야합니다. Main.java
클래스 :응용 프로그램이 강제 종료됩니다. SQLite 관련 일 수 있습니다.
package se.maxallan.birdsound;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Main extends Activity {
private SpeciesDbAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dbHelper = new SpeciesDbAdapter(this);
dbHelper.open();
dbHelper.insertSomeSpecies();
displayListView();
}
private void displayListView() {
Cursor cursor = dbHelper.fetchAllSpecies();
String[] columns = new String[]{
SpeciesDbAdapter.DB_SCF_NAME,
SpeciesDbAdapter.DB_T_TRANS
};
int[] to = new int[]{
R.id.scf,
R.id.trans
};
dataAdapter = new SimpleCursorAdapter(this,R.layout.specierow,cursor,columns,to);
ListView listView = (ListView) findViewById(R.id.specieList);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
}
나는 오류가 SpeciesDbAdapter.java
라는 이름의이 클래스에 위치하고 있습니다 생각합니다.
package se.maxallan.birdsound;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class SpeciesDbAdapter extends SQLiteOpenHelper{
public static final String DB_SPECIE_ID = "_id";
public static final String DB_SCF_NAME = "scf_name";
public static final String DB_T_LANG = "lang";
public static final String DB_T_TRANS = "translated";
private static SpeciesDbAdapter mDbHelp;
private static SQLiteDatabase mDb;
private static final String DB_NAME = "database.db";
private static final String DB_TBL_SPECIES = "species";
private static final String DB_TBL_TRANSLATIONS = "translations";
//private static final String DB_TBL_SOUNDS = "sounds";
private static final int DB_VERSION = 1;
private final Context mCtx;
private static String DB_CREATE =
"CREATE TABLE if not exists species ("+DB_SPECIE_ID +" integer PRIMARY KEY autoincrement, "+DB_SCF_NAME+" , UNIQUE ("+DB_SPECIE_ID +"));" +
"CREATE TABLE if not exists translations (tid integer PRIMARY KEY autoincrement, "+DB_SPECIE_ID +" integer, "+DB_T_TRANS+", "+DB_T_LANG+", UNIQUE (tid));";
public SpeciesDbAdapter(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.mCtx = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.w("DATABAS", DB_CREATE);
db.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
//Open the database
public SpeciesDbAdapter open() throws SQLException {
mDbHelp = new SpeciesDbAdapter(mCtx);
mDb = mDbHelp.getWritableDatabase();
return this;
}
//Close the connection
public void close() {
if (mDbHelp != null) {
mDbHelp.close();
}
}
public void addSpecie(String scf, String translation, String lang){
ContentValues specieValues = new ContentValues();
ContentValues translationsValues = new ContentValues();
specieValues.put(DB_SCF_NAME, scf);
int LastInsertedId = (int) mDb.insert(DB_TBL_SPECIES, null, specieValues);
translationsValues.put(DB_T_LANG, lang);
translationsValues.put(DB_T_TRANS, translation);
translationsValues.put(DB_SPECIE_ID, LastInsertedId);
mDb.insert(DB_TBL_TRANSLATIONS, null, translationsValues);
}
public Cursor fetchAllSpecies() {
Cursor mCursor = mDb.query(DB_TBL_SPECIES, new String[] {DB_SPECIE_ID, DB_SCF_NAME},null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public void insertSomeSpecies() {
addSpecie("Cygnus olor", "Knölsvan", "sv");
addSpecie("Cygnus cygnus", "Sångsvan", "sv");
}
}
나는 뭔가를 오해 한 것 같습니다. 그리고 CatLog 당신은 두 가지를 조회
String createSpecies = "CREATE TABLE if not exists species ("+DB_SPECIE_ID +" integer PRIMARY KEY autoincrement, "+DB_SCF_NAME+" , UNIQUE ("+DB_SPECIE_ID +"));"
String createTranslations = "CREATE TABLE if not exists translations (tid integer PRIMARY KEY autoincrement, "+DB_SPECIE_ID +" integer, "+DB_T_TRANS+", "+DB_T_LANG+", UNIQUE (tid));";
@Override
public void onCreate(SQLiteDatabase db)
{
Log.w("DATABAS", DB_CREATE);
db.execSQL(createSpecies);
db.execSQL(createTranslations);
}
아직 냉동 상태입니다 ... –