モトローラ デファイ ミニを使用した Android システム (2.3.6) の SQLite に問題があります。パーソナル クラスでコーディングした open メソッドでデータベースを開くと、エラーが発生しますsqlite_config failed error code = 21 this should never occur
。
しかし、私が理解していないのは、HTC Wildfire S ではこれが正常に機能し、エミュレーターでも機能するということです。このエラーがわかりません。
介入OpenHelper
package com.example.telegestion;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class InterventionOpenHelper extends SQLiteOpenHelper {
// Version de la base de données
private static final int DATABASE_VERSION = 1;
//Nom de la Base
private static final String TELEGESTION_BASE_NAME ="telegestion.db";
//Nom de la table
public static final String INTERVENTION_TABLE_NAME="Intervention";
//Descriptions des colonnes
public static final String COLUMN_IDLIGNE = "idligne";
public static final int NUM_COLUMN_IDLIGNE=0;
public static final String COLUMN_DATE_CREATION= "dateCreation";
public static final int NUM_COLUMN_DATE_CREATION=1;
public static final String COLUMN_HEURE_CREATION="heureCreation";
public static final int NUM_COLUMN_HEURE_CREATION=2;
public static final String COLUMN_CODE_ASSO="codeAsso";
public static final int NUM_COLUMN_CODE_ASSO=3;
public static final String COLUMN_ID_PA="idPA";
public static final int NUM_COLUMN_ID_PA=4;
public static final String COLUMN_NOM_PRENOM= "nomPrenom";
public static final int NUM_COLUMN_NOM_PRENOM=5;
public static final String COLUMN_ACTION_CODE ="actionCode";
public static final int NUM_COLUMN_ACTION_CODE=6;
public static final String COLUMN_MOTIF_PAS_CODE ="motifPasCode";
public static final int NUM_COLUMN_MOTIF_PAS_CODE=7;
public static final String COLUMN_DATE_INTERVENTION ="dateIntervention";
public static final int NUM_COLUMN_DATE_INTERVENTION=8;
public static final String COLUMN_HEURE_INTERVENTION="heureIntervention";
public static final int NUM_COLUMN_HEURE_INTERVENTION=9;
public static final String COLUMN_CODE_PREST="codePrest";
public static final int NUM_COLUMN_CODE_PREST=10;
public static final String COLUMN_A_POSITIONNER="aPositionner";
public static final int NUM_COLUMN_A_POSITIONNER=11;
public static final String COLUMN_LONGITUDE="longitude";
public static final int NUM_COLUMN_LONGITUDE=12;
public static final String COLUMN_LATTITUDE="lattitude";
public static final int NUM_COLUMN_LATTITUDE=13;
public static final String COLUMN_DATE_GPS="dateGPS";
public static final int NUM_COLUMN_DATE_GPS=14;
public static final String COLUMN_HEURE_GPS="heureGPS";
public static final int NUM_COLUMN_HEURE_GPS=15;
public static final String COLUMN_KM="km";
public static final int NUM_COLUMN_KM=16;
public static final String COLUMN_ANNULER="annuler";
public static final int NUM_COLUMN_ANNULER=17;
public static final String COLUMN_DATE_ANNULER="dateAnnuler";
public static final int NUM_COLUMN_DATE_ANNULER=18;
public static final String COLUMN_HEURE_ANNULER="heureAnnuler";
public static final int NUM_COLUMN_HEURE_ANNULER=19;
public static final String COLUMN_A_TRANSMETTRE="aTransmettre";
public static final int NUM_COLUMN_A_TRANSMETTRE=20;
public static final String COLUMN_DATE_TRANSMIS="dateTransmis";
public static final int NUM_COLUMN_DATE_TRANSMIS=21;
public static final String COLUMN_HEURE_TRANSMIS="heureTransmis";
public static final int NUM_COLUMN_HEURE_TRANSMIS=22;
public InterventionOpenHelper(Context context, CursorFactory factory) {
super(context, TELEGESTION_BASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(REQUETE_CREATION_BDD);
}
public void onConfigure(SQLiteDatabase db){
System.out.println("dans le configure");
}
public void onOpen(SQLiteDatabase db){
System.out.println("dans le open");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// Lorsque l'on change le numéro de version de la base on supprime la
// table puis on la recrée
db.execSQL("DROP TABLE " + INTERVENTION_TABLE_NAME + ";");
onCreate(db);
}
// Requête SQL pour la création da la base
private static final String REQUETE_CREATION_BDD =
"CREATE TABLE "
+ INTERVENTION_TABLE_NAME + " ("
+ COLUMN_IDLIGNE + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_DATE_CREATION + " TEXT NOT NULL, "
+ COLUMN_HEURE_CREATION + " TEXT NOT NULL, "
+ COLUMN_CODE_ASSO + " TEXT NOT NULL, "
+ COLUMN_ID_PA + " INTEGER NOT NULL, "
+ COLUMN_NOM_PRENOM + " TEXT NOT NULL, "
+ COLUMN_ACTION_CODE + " TEXT NOT NULL, "
+ COLUMN_MOTIF_PAS_CODE + " TEXT NOT NULL, "
+ COLUMN_DATE_INTERVENTION + " TEXT NOT NULL, "
+ COLUMN_HEURE_INTERVENTION + " TEXT NOT NULL, "
+ COLUMN_CODE_PREST + " TEXT NOT NULL, "
+ COLUMN_A_POSITIONNER + " INTEGER NOT NULL, "
+ COLUMN_LATTITUDE + " REAL NOT NULL, "
+ COLUMN_LONGITUDE + " REAL NOT NULL, "
+ COLUMN_DATE_GPS + " TEXT NOT NULL, "
+ COLUMN_HEURE_GPS + " TEXT NOT NULL, "
+ COLUMN_KM + " TEXT NOT NULL, "
+ COLUMN_ANNULER + " INTEGER NOT NULL, "
+ COLUMN_DATE_ANNULER + " TEXT NOT NULL, "
+ COLUMN_HEURE_ANNULER + " TEXT NOT NULL, "
+ COLUMN_A_TRANSMETTRE + " INTEGER NOT NULL, "
+ COLUMN_DATE_TRANSMIS + " TEXT NOT NULL, "
+ COLUMN_HEURE_TRANSMIS + " TEXT NOT NULL);";
}
介入リポジトリ
package com.example.telegestion;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
public class InterventionRepository extends Repository<Intervention> {
public InterventionRepository(Context context) {
sqLiteOpenHelper = new InterventionOpenHelper(context, null);
}
@Override
public List<Intervention> GetAll() {
// TODO Auto-generated method stub
Cursor cursor = maBDD.query(
InterventionOpenHelper.INTERVENTION_TABLE_NAME, new String[] {
InterventionOpenHelper.COLUMN_IDLIGNE,
InterventionOpenHelper.COLUMN_DATE_CREATION,
InterventionOpenHelper.COLUMN_HEURE_CREATION,
InterventionOpenHelper.COLUMN_CODE_ASSO,
InterventionOpenHelper.COLUMN_ID_PA,
InterventionOpenHelper.COLUMN_NOM_PRENOM,
InterventionOpenHelper.COLUMN_ACTION_CODE,
InterventionOpenHelper.COLUMN_MOTIF_PAS_CODE,
InterventionOpenHelper.COLUMN_DATE_INTERVENTION,
InterventionOpenHelper.COLUMN_HEURE_INTERVENTION,
InterventionOpenHelper.COLUMN_CODE_PREST,
InterventionOpenHelper.COLUMN_A_POSITIONNER,
InterventionOpenHelper.COLUMN_LATTITUDE,
InterventionOpenHelper.COLUMN_LONGITUDE,
InterventionOpenHelper.COLUMN_DATE_GPS,
InterventionOpenHelper.COLUMN_HEURE_GPS,
InterventionOpenHelper.COLUMN_KM,
InterventionOpenHelper.COLUMN_ANNULER,
InterventionOpenHelper.COLUMN_DATE_ANNULER,
InterventionOpenHelper.COLUMN_HEURE_ANNULER,
InterventionOpenHelper.COLUMN_A_TRANSMETTRE,
InterventionOpenHelper.COLUMN_DATE_TRANSMIS,
InterventionOpenHelper.COLUMN_HEURE_TRANSMIS }, null,
null, null, null, null);
return ConvertCursorToListObject(cursor);
}
@Override
public Intervention GetById(int id) {
Cursor cursor = maBDD.query(
InterventionOpenHelper.INTERVENTION_TABLE_NAME, new String[] {
InterventionOpenHelper.COLUMN_IDLIGNE,
InterventionOpenHelper.COLUMN_DATE_CREATION,
InterventionOpenHelper.COLUMN_HEURE_CREATION,
InterventionOpenHelper.COLUMN_CODE_ASSO,
InterventionOpenHelper.COLUMN_ID_PA,
InterventionOpenHelper.COLUMN_NOM_PRENOM,
InterventionOpenHelper.COLUMN_ACTION_CODE,
InterventionOpenHelper.COLUMN_MOTIF_PAS_CODE,
InterventionOpenHelper.COLUMN_DATE_INTERVENTION,
InterventionOpenHelper.COLUMN_HEURE_INTERVENTION,
InterventionOpenHelper.COLUMN_CODE_PREST,
InterventionOpenHelper.COLUMN_A_POSITIONNER,
InterventionOpenHelper.COLUMN_LATTITUDE,
InterventionOpenHelper.COLUMN_LONGITUDE,
InterventionOpenHelper.COLUMN_DATE_GPS,
InterventionOpenHelper.COLUMN_HEURE_GPS,
InterventionOpenHelper.COLUMN_KM,
InterventionOpenHelper.COLUMN_ANNULER,
InterventionOpenHelper.COLUMN_DATE_ANNULER,
InterventionOpenHelper.COLUMN_HEURE_ANNULER,
InterventionOpenHelper.COLUMN_A_TRANSMETTRE,
InterventionOpenHelper.COLUMN_DATE_TRANSMIS,
InterventionOpenHelper.COLUMN_HEURE_TRANSMIS },
InterventionOpenHelper.COLUMN_IDLIGNE + "=?",
new String[] { String.valueOf(id) }, null, null, null);
return ConvertCursorToObject(cursor);
}
@Override
public void Save(Intervention entite) {
DateFormat sfDate = new SimpleDateFormat("yyyy-MM-dd");
DateFormat sfHeure = new SimpleDateFormat("HH:mm:ss");
String sDateCreation = sfDate.format(entite.getDateCreation());
String sHeureCreation = sfHeure.format(entite.getHeureCreation());
String sDateInter = sfDate.format(entite.getDateIntervention());
String sHeureInter = sfHeure.format(entite.getHeureIntervention());
String sDateGPS = sfDate.format(entite.getDateGPS());
String sHeureGPS = sfHeure.format(entite.getHeureGPS());
String sDateAnnuler = sfDate.format(entite.getDateAnnuler());
String sHeureAnnuler = sfHeure.format(entite.getHeureAnnuler());
String sDateTransmis = sfDate.format(entite.getDateTransmis());
String sHeureTransmis = sfHeure.format(entite.getHeureTransmis());
// TODO Auto-generated method stub
ContentValues contentValues = new ContentValues();
contentValues.put(InterventionOpenHelper.COLUMN_IDLIGNE,
entite.getIdLigne());
contentValues.put(InterventionOpenHelper.COLUMN_DATE_CREATION,
sDateCreation);
contentValues.put(InterventionOpenHelper.COLUMN_HEURE_CREATION,
sHeureCreation);
contentValues.put(InterventionOpenHelper.COLUMN_CODE_ASSO,
entite.getCodeAsso());
contentValues
.put(InterventionOpenHelper.COLUMN_ID_PA, entite.getIdPA());
contentValues.put(InterventionOpenHelper.COLUMN_NOM_PRENOM,
entite.getNomPrenom());
contentValues.put(InterventionOpenHelper.COLUMN_ACTION_CODE,
entite.getActionCode());
contentValues.put(InterventionOpenHelper.COLUMN_MOTIF_PAS_CODE,
entite.getMotifPasCode());
contentValues.put(InterventionOpenHelper.COLUMN_DATE_INTERVENTION,
sDateInter);
contentValues.put(InterventionOpenHelper.COLUMN_HEURE_INTERVENTION,
sHeureInter);
contentValues.put(InterventionOpenHelper.COLUMN_CODE_PREST,
entite.getCodePrest());
contentValues.put(InterventionOpenHelper.COLUMN_A_POSITIONNER,
entite.isaPositionner());
contentValues.put(InterventionOpenHelper.COLUMN_LATTITUDE,
entite.getLattitude());
contentValues.put(InterventionOpenHelper.COLUMN_LONGITUDE,
entite.getLongitude());
contentValues.put(InterventionOpenHelper.COLUMN_DATE_GPS, sDateGPS);
contentValues.put(InterventionOpenHelper.COLUMN_HEURE_GPS, sHeureGPS);
contentValues.put(InterventionOpenHelper.COLUMN_KM, entite.getKm());
contentValues.put(InterventionOpenHelper.COLUMN_ANNULER,
entite.isAnnuler());
contentValues.put(InterventionOpenHelper.COLUMN_DATE_ANNULER,
sDateAnnuler);
contentValues.put(InterventionOpenHelper.COLUMN_HEURE_ANNULER,
sHeureAnnuler);
contentValues.put(InterventionOpenHelper.COLUMN_A_TRANSMETTRE,
entite.isaTransmettre());
contentValues.put(InterventionOpenHelper.COLUMN_DATE_TRANSMIS,
sDateTransmis);
contentValues.put(InterventionOpenHelper.COLUMN_HEURE_TRANSMIS,
sHeureTransmis);
maBDD.insert(InterventionOpenHelper.INTERVENTION_TABLE_NAME, null,
contentValues);
}
@Override
public void Update(Intervention entite) {
// TODO Auto-generated method stub
}
@Override
public void Delete(int id) {
// TODO Auto-generated method stub
maBDD.delete(InterventionOpenHelper.INTERVENTION_TABLE_NAME,
InterventionOpenHelper.COLUMN_IDLIGNE + "=?",
new String[] { String.valueOf(id) });
}
@Override
public List<Intervention> ConvertCursorToListObject(Cursor c) {
List<Intervention> liste = new ArrayList<Intervention>();
// Si la liste est vide
if (c.getCount() == 0)
return liste;
// position sur le premier item
c.moveToFirst();
// Pour chaque item
do {
Intervention inter = ConvertCursorToObject(c);
liste.add(inter);
} while (c.moveToNext());
// Fermeture du curseur
c.close();
return liste;
}
@Override
public Intervention ConvertCursorToObject(Cursor c) {
Intervention inter = null;
try {
Date cursorDateCrea = stringToDate(
c.getString(InterventionOpenHelper.NUM_COLUMN_DATE_CREATION),
"yyyy-MM-dd");
Date cursorHeureCrea = stringToDate(
c.getString(InterventionOpenHelper.NUM_COLUMN_HEURE_CREATION),
"HH:mm:ss");
Date cursorDateInter = stringToDate(
c.getString(InterventionOpenHelper.NUM_COLUMN_DATE_INTERVENTION),
"yyyy-MM-dd");
Date cursorHeureInter = stringToDate(
c.getString(InterventionOpenHelper.NUM_COLUMN_HEURE_INTERVENTION),
"HH:mm:ss");
Date cursorDateGPS = stringToDate(
c.getString(InterventionOpenHelper.NUM_COLUMN_DATE_GPS),
"yyyy-MM-dd");
Date cursorHeureGPS = stringToDate(
c.getString(InterventionOpenHelper.NUM_COLUMN_HEURE_GPS),
"HH:mm:ss");
Date cursorDateAnnuler = stringToDate(
c.getString(InterventionOpenHelper.NUM_COLUMN_DATE_ANNULER),
"yyyy-MM-dd");
Date cursorHeureAnnuler = stringToDate(
c.getString(InterventionOpenHelper.NUM_COLUMN_HEURE_ANNULER),
"HH:mm:ss");
Date cursorDateTransmis = stringToDate(
c.getString(InterventionOpenHelper.NUM_COLUMN_DATE_TRANSMIS),
"yyyy-MM-dd");
Date cursorHeureTransmis = stringToDate(
c.getString(InterventionOpenHelper.NUM_COLUMN_HEURE_TRANSMIS),
"HH:mm:ss");
inter = new Intervention(
c.getInt(InterventionOpenHelper.NUM_COLUMN_IDLIGNE),
cursorDateCrea,
cursorHeureCrea,
c.getString(InterventionOpenHelper.NUM_COLUMN_CODE_ASSO),
c.getInt(InterventionOpenHelper.NUM_COLUMN_ID_PA),
c.getString(InterventionOpenHelper.NUM_COLUMN_NOM_PRENOM),
c.getString(InterventionOpenHelper.NUM_COLUMN_ACTION_CODE),
c.getString(InterventionOpenHelper.NUM_COLUMN_MOTIF_PAS_CODE),
cursorDateInter,
cursorHeureInter,
c.getString(InterventionOpenHelper.NUM_COLUMN_CODE_PREST),
c.getInt(InterventionOpenHelper.NUM_COLUMN_A_POSITIONNER) == 1,
c.getDouble(InterventionOpenHelper.NUM_COLUMN_LONGITUDE),
c.getDouble(InterventionOpenHelper.NUM_COLUMN_LATTITUDE),
cursorDateGPS,
cursorHeureGPS,
c.getString(InterventionOpenHelper.NUM_COLUMN_KM),
c.getInt(InterventionOpenHelper.NUM_COLUMN_ANNULER) == 1,
cursorDateAnnuler,
cursorHeureAnnuler,
c.getInt(InterventionOpenHelper.NUM_COLUMN_A_TRANSMETTRE) == 1,
cursorDateTransmis, cursorHeureTransmis);
} catch (Exception e) {
e.printStackTrace();
}
return inter;
}
@Override
public Intervention ConvertCursorToOneObject(Cursor c) {
c.moveToFirst();
Intervention inter = ConvertCursorToObject(c);
c.close();
return inter;
}
public static Date stringToDate(String sDate, String sFormat)
throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat(sFormat);
return sdf.parse(sDate);
}
}
リポジトリ
package com.example.telegestion;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public abstract class Repository<T> implements IRepository<T> {
// Base de données
protected SQLiteDatabase maBDD;
protected SQLiteOpenHelper sqLiteOpenHelper;
public Repository() {
}
/**
* Ouverture de la connection
*/
public void Open() {
try {
maBDD = sqLiteOpenHelper.getWritableDatabase();
} catch (Exception e) {
Log.e("Error", e.toString());
}
}
/**
* Fermeture de la connection
*/
public void Close() {
maBDD.close();
}
}
主な活動
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InterventionOpenHelper ir = new InterventionOpenHelper(this, null);
ir.Open();
ir.Close();
}