所以我看到了很多关于这个主题的答案,但是既没有奏效,也没有真正理解该怎么做! xD所以,如果你通过链接一个回答那么好的主题来回答!
我有一个MainActivity,我显示我的东西和所有,以及一个类dbHandler。
在我的dbHandler类中,一些函数正在调用getWritableDatabase(),当它们在我的MainActivity中被调用时,我的应用程序崩溃了:/,我看到我不应该在dbHandler中使用它因为它无法被调用recursivly blabla但我不知道它被称为recursivly ... xD
以下是名为Accueil的MainActivity还有更多的代码,但是我剪掉了无用的代码,而且应该有法语单词这是正常的我做了很多frenglish xD
public class Accueil extends AppCompatActivity {
TextView textDB;
dbHandler db_handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accueil);
textDB = (TextView) findViewById(R.id.textDB);
db_handler = new dbHandler(this, null, null, 1);
Alerte alerte = new Alerte("feu");
db_handler.addAlerte(alerte);
//printDatabase();
}
public void printDatabase(){
String dbString = db_handler.databaseToString();
textDB.setText(dbString);
}
}
和dbHandler
package com.example.yorokobii.popalerte;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class dbHandler extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Alertes.db";
public static final String TABLE_ALERTES = "Alertes";
public static final String COLUMN_ID = "_id ";
public static final String COLUMN_ALERTENAME = "alertename ";
public dbHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + DATABASE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_ALERTENAME + " TEXT "
+ ");";
db.execSQL(query);/*
ContentValues values = new ContentValues();
values.put(COLUMN_ALERTENAME, "test");
db.insert(TABLE_ALERTES, null, values);*/
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);
onCreate(db);
}
//fonction qui ajoute une alerte a la base de donnée
public void addAlerte(Alerte alerte){
ContentValues values = new ContentValues();
values.put(COLUMN_ALERTENAME, alerte.get_alertename());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_ALERTES, null, values); //effectue la requète insert sur TABLE_ALERTES avec les valeur rentrées dans value
db.close();
}
//fonction qui supprime une alerte en fonction de son nom (plus tard son état surement)
public void deleteAlerte(String alertename){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_ALERTES + "WHERE " + COLUMN_ALERTENAME + "=\"" + alertename + "\";");
db.close();
}
//fonction qui supprime toutes les alertes
public void deleteAlerte(){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_ALERTES + "WHERE 1");
db.close();
}
//fonction qui affiche les alertes dans une chaine de caractère
public String databaseToString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_ALERTES;
//créé un cuseur pour la requète
Cursor c = db.rawQuery(query, null);
c.moveToFirst();//place le curseur a la premiere colonne du resultat de la requete
while(!c.isAfterLast()){ //tant qu'il y a des resultats a traiter
if(c.getString(c.getColumnIndex(COLUMN_ALERTENAME)) != null){ //si le nom de l'alerte n'est pas null on l'affiche
dbString = c.getString(c.getColumnIndex(COLUMN_ALERTENAME));
dbString += "\n";
}
}
db.close();
return dbString;
}
}
LogCat
Caused by: android.database.sqlite.SQLiteException: unknown database Alertes (code 1): , while compiling: CREATE TABLE Alertes.db(_id INTEGER PRIMARY KEY AUTOINCREMENT, alertename TEXT );
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at com.example.yorokobii.popalerte.dbHandler.onCreate(dbHandler.java:27)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.yorokobii.popalerte.Accueil.addAlerte(Accueil.java:173)
at com.example.yorokobii.popalerte.Accueil.onCreate(Accueil.java:115)
at android.app.Activity.performCreate(Activity.java:6237)
答案 0 :(得分:1)
String query = "CREATE TABLE " + DATABASE_NAME + "("
数据库名称不是表名。