Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I have the Database bur error: SQL(query) error or missing database

I created a database there is no problem, I can show it add data manually, but in the if else method it is not adding any value, I can see the values fit to the if method in the log but I think the problem is execSQL method I can not call it.

Near the bottom of second code I marked with the // line

Error:

android.database.sqlite.SQLiteException: near "s": syntax error (code 1): , while compiling: INSERT INTO myTable1(id, FAVCampaignName, FAVCampaigncampaignStartDate, FAVCampaigncampaignEndDate) VALUES(NULL,'Starbucks's','2019-04-01','2019-04-19'); ################################################################# Error Code : 1 (SQLITE_ERROR) Caused By : SQL(query) error or missing database. (near "de": syntax error (code 1): , while compiling: INSERT INTO myTable1(id, FAVCampaignName, FAVCampaigncampaignStartDate, FAVCampaigncampaignEndDate) VALUES(NULL,'Starbucks's','2019-04-01','2019-04-19');) ################################################################# at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1062)

Code:

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);");
}

Code2:

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 {
                    }
                }
            });
}

1 Answers

FAVCampaignName contains a single quote so the database thinks the string ends at köy' and does not know what to do with de...

'Starbucks's 2.kahve %50 indirimli'

Either escape all single quotes with a \ or use a parameterized query.

like image 146
Alien Technology Avatar answered Mar 27 '26 16:03

Alien Technology