我有数据库bur错误:SQL(query)错误或缺少数据库

时间:2019-04-26 14:34:31

标签: java android sqlite

我创建的数据库没有问题,我可以显示它手动添加数据,但是在if else方法中它没有添加任何值,我可以在日志中看到适合if方法的值,但是我认为问题是我无法调用execSQL方法。

在我用//行标记的第二个代码的底部附近

错误:

  

android.database.sqlite.SQLiteException:靠近“ s”:语法错误(代码1):,同时编译:INSERT INTO myTable1(id,FAVCampaignName,FAVCampaigncampaignStartDate,FAVCampaigncampaignEndDate)VALUES(NULL,'Starbucks's,'2019- 04-01','2019-04-19');       ################################################ ###############       错误代码:1(SQLITE_ERROR)       引起原因:SQL(query)错误或数据库丢失。           (靠近“ de”:语法错误(代码1):,编译时:插入myTable1(id,FAVCampaignName,FAVCampaigncampaignStartDate,FAVCampaigncampaignEndDate)VALUES(NULL,'星巴克','2019-04-01','2019-04- 19');)       ################################################ ###############           在android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法)           在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1062)

代码:

public void DBCreateFavCampaign(){
    SQLITEDATABASEFavCampaign = openOrCreateDatabase("FavCampaign3", Context.MODE_PRIVATE, null);
    SQLITEDATABASEFavCampaign.execSQL("CREATE TABLE IF NOT EXISTS myTable1(id INTEGER PRIMARY KEY AUTOINCREMENT, FAVCampaignName VARCHAR,FAVCampaigncampaignStartDate VARCHAR, FAVCampaigncampaignEndDate VARCHAR);");
}

代码2:

public void readFirestore() {
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    db.collection("campaigns")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                private String FSname,FScityLAT,FScityLONG,FScampaignStartDate,FScampaignEndDate;

                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful() && task.getResult() != null) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            String name = document.getString("name");
                            String cityLAT = document.getString("cityLAT");
                            String cityLONG = document.getString("cityLONG");
                            String campaignStartDate = document.getString("campaignStartDate");
                            String campaignEndDate = document.getString("campaignEndDate");

                            this.FSname = name;
                            this.FScityLAT = cityLAT;
                            this.FScityLONG = cityLONG;
                            this.FScampaignStartDate = campaignStartDate;
                            this.FScampaignEndDate = campaignEndDate;

                            String FS_FAVCurrentLocationLAT = List_FAVCurrentLocationLAT;
                            String FS_FAVCurrentLocationLONG = List_FAVCurrentLocationLONG;

                            double FS_FAVCurrentLocationLAT_double = Double.parseDouble(FS_FAVCurrentLocationLAT); 
                            double FS_FAVCurrentLocationLONG_double = Double.parseDouble(FS_FAVCurrentLocationLONG); 

                            double FScityLAT_double = Double.parseDouble(FScityLAT);  
                            double FScityLONG_double = Double.parseDouble(FScityLONG); 


                            double theta = FScityLONG_double - FS_FAVCurrentLocationLONG_double;
                            double dist = Math.sin(Math.toRadians(FS_FAVCurrentLocationLAT_double)) * Math.sin(Math.toRadians(FScityLAT_double)) + Math.cos(Math.toRadians(FS_FAVCurrentLocationLAT_double)) * Math.cos(Math.toRadians(FScityLAT_double)) * Math.cos(Math.toRadians(theta));
                            dist = Math.acos(dist);
                            dist = Math.toDegrees(dist);
                            dist = dist * 60 * 1.1515;
                            dist = dist * 1.609344;

                            Log.i("hello",""+dist); //OK it writes to the log


                            if (dist <= 0.5) // 
                            {
                                Log.i("near",""+dist); // Ok
                                DBCreateFavCampaign();

                                SQLiteQueryFavCampaign = "INSERT INTO myTable1(id, FAVCampaignName, FAVCampaigncampaignStartDate, FAVCampaigncampaignEndDate) VALUES(NULL,'"+FSname+"','"+FScampaignStartDate+"','"+FScampaignEndDate+"');";
                                SQLITEDATABASEFavCampaign.execSQL(SQLiteQueryFavCampaign); //execSQL gives the error

                                Toast.makeText(CampaignActivity.this,"FAV OK", Toast.LENGTH_SHORT).show();
                            }
                        }
                    } else {
                    }
                }
            });
}

3 个答案:

答案 0 :(得分:5)

FAVCampaignName包含一个单引号,因此数据库认为该字符串以köy结尾,并且不知道如何处理de ...

'Starbucks's 2.kahve %50 indirimli'

要么用\转义所有单引号,要么使用参数化查询。

答案 1 :(得分:2)

'Starbucks's' ...

如果您发现sql认为您在字符串中以该字符'结尾。尝试将其删除。 Sqlite认为您的字符串是:Starbucks,然后de s是语法错误。

在该字符串中,请添加\'而不是',您就可以了。

答案 2 :(得分:2)

正如其他答案所说,问题在于变量FSname的值为"Starbucks's",其中包含引号。这会导致SQL引擎将值解释为"Starbucks",并且由于无法解析字符串的其余部分,因此会导致错误。

尝试通过以下方式更改查询:

SQLiteQueryFavCampaign = "INSERT INTO myTable1(id, FAVCampaignName, FAVCampaigncampaignStartDate, FAVCampaigncampaignEndDate) VALUES(NULL,\""+FSname+"\",'"+FScampaignStartDate+"','"+FScampaignEndDate+"');";

请注意,我用双引号替换了FSname旁的简单引号。