E / SQLiteLog:(1)在“(”附近:语法

时间:2018-09-18 22:51:06

标签: android sqlite compiler-errors android-sqlite logcat

我尝试将数据保存在我的应用程序数据库中一段时间​​,但仍然遇到相同的问题。这是我的代码。

在数据库页面中:

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)

谁能告诉我我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

我认为问题在于名为 Distance(Km)的列,以便使用此类列名。您必须将其围起来例如

// Use this for initialization
void Start()
{
    //Calculate and store the offset value by getting the distance between the player's position and camera's position.
    offset = transform.position - player.transform.position;
}

// LateUpdate is called after Update each frame
void LateUpdate()
{
    // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
    transform.position = player.transform.position + offset;
}

您可以更改 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
    );

请注意,如果存在该列,则在定义表时必须将其名称括起来。

但是,我建议将列重命名为 Distance_Km

还有一些您也可以使用的封闭字符

  • “距离(公里)” ,即包含在
  • [距离(Km)] ,即用 []
  • 括起来
  • “距离(公里)” ,即用''
  • 括起来

此问题在SQL As Understood By SQLite - SQLite Keywords

涵盖