我尝试将数据保存在我的应用数据库中一段时间,但我仍然遇到同样的问题。这是我的代码的样子。
在数据库页面中:
public void insererTrajetTest (String et_VilleD, String et_VilleA, String
et_LieuDepart, String et_LieuArrivee,String et_dateDepartAller, String
et_heureDepartAller, String et_flexibilite, String et_bagage, String et_detour,
String et_iduser, String et_trajet, String et_commentaire, String et_place,
String et_dateArriveeAller, String et_heureArriveeAller, String et_prix, String
et_distance, String et_duree) {
db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME3;
Cursor cursor = db.rawQuery(query, null);
ContentValues values = new ContentValues();
values.put(COLUMN_VILLE_DEPART, et_VilleD);
values.put(COLUMN_VILLE_ARRIVEE, et_VilleA);
values.put(COLUMN_LIEU_DEPART, et_LieuDepart);
values.put(COLUMN_LIEU_ARRIVEE, et_LieuArrivee);
values.put(COLUMN_DATE_DEPART, et_dateDepartAller);
values.put(COLUMN_HEURE_DEPART, et_heureDepartAller);
values.put(COLUMN_FLEXIBILITE, et_flexibilite);
values.put(COLUMN_BAGAGE, et_bagage);
values.put(COLUMN_DETOUR, et_detour);
values.put(COLUMN_ID_CHAUFFEUR, et_iduser);
values.put(COLUMN_TRAJET, et_trajet);
values.put(COLUMN_COMMENTAIRE, et_commentaire);
values.put(COLUMN_PLACES_PROPOSEES, et_place);
values.put(COLUMN_PLACES_DISPONIBLES, et_place);
values.put(COLUMN_DATE_ARRIVEE, et_dateArriveeAller);
values.put(COLUMN_HEURE_ARRIVEE, et_heureArriveeAller);
values.put(COLUMN_PRIX, et_prix);
values.put(COLUMN_DISTANCE, et_distance);
values.put(COLUMN_DUREE, et_duree);
db.insert(TABLE_NAME3, null, values);
Log.d("Base de données", "Un nouveau voyage a été créé...");
db.close();
cursor.close();
}
但是我总是在logcat中得到以下内容:
09-18 22:15:49.986 4108-4427/cm.opep.opep E/SQLiteLog: (1) near "(": syntax error
09-18 22:15:49.990 4108-4427/cm.opep.opep E/SQLiteDatabase: Error inserting VilleDépart=Abong-Mbang Trajet=Abong-Mbang --> Ambam: HeureDépart=23:45 DateDépart=2018-09-28 Flexibilité=+/- 30 minutes HeureArrivée=05:59 Bagage=Petit Commentaire=Nine LieuArrivée=Er VilleArrivée=Ambam Durée=06 h 14 min DateArrivée=2018-09-29 LieuDépart=Az IDChauffeur=5 Détour=+/- 15 minutes Prix=3100 Distance(Km)=422 PlacesDisponibles=5 PlacesProposées=5
android.database.sqlite.SQLiteException: near "(": syntax error (code 1): , while compiling: INSERT INTO Voyage(VilleDépart,Trajet,HeureDépart,DateDépart,Flexibilité,HeureArrivée,Bagage,Commentaire,LieuArrivée,VilleArrivée,Durée,DateArrivée,LieuDépart,IDChauffeur,Détour,Prix,Distance(Km),PlacesDisponibles,PlacesProposées) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at cm.opep.opep.DatabaseHelper.insererTrajetTest(DatabaseHelper.java:1328)
at cm.opep.opep.fragment.BackGroundTask.doInBackground(BackGroundTask.java:56)
at cm.opep.opep.fragment.BackGroundTask.doInBackground(BackGroundTask.java:12)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
谁能告诉我我的代码出了什么问题?
我认为问题在于使用名为Distance(Km)的列来使用这样的列名。你必须把它包括起来,例如
Prix,`Distance(Km)`,PlacesDisponibles,
你可以做这个海湾改变COLUMN_DISTANCE
定义使用
COLUMN_DISTANCE = "`Distance(Km)`"
这将导致:
INSERT INTO Voyage(VilleDépart,Trajet,HeureDépart,DateDépart,Flexibilité,HeureArrivée,Bagage,Commentaire,LieuArrivée,VilleArrivée,Durée,DateArrivée,LieuDépart,IDChauffeur,Détour,Prix,`Distance(Km)`,PlacesDisponibles,PlacesProposées) VALUES (
'Abong-Mbang','Abong-Mbang --> Ambam:','23:45','2018-09-28','+/- 30 minutes','05:59','Petit','Nine','Er','Ambam','06 h 14 min','2018-09-29',
'Az',5,'+/- 15 minutes',3100,422,5,5
);
其工作原理如下: -
INSERT INTO Voyage(VilleDépart,Trajet,HeureDépart,DateDépart,Flexibilité,HeureArrivée,Bagage,Commentaire,LieuArrivée,VilleArrivée,Durée,DateArrivée,LieuDépart,IDChauffeur,Détour,Prix,`Distance(Km)`,PlacesDisponibles,PlacesProposées) VALUES (
'Abong-Mbang','Abong-Mbang --> Ambam:','23:45','2018-09-28','+/- 30 minutes','05:59','Petit','Nine','Er','Ambam','06 h 14 min','2018-09-29',
'Az',5,'+/- 15 minutes',3100,422,5,5
)
> Affected rows: 1
> Time: 0.23s
注意列存在,然后在定义表时必须包含它的名称。
但是,我建议将列重命名为Distance_Km。
您也可以使用其他封闭字符