I have database bur error: SQL(query) error or missing database
I created a database without a problem, I can show it adding data manually, but in the if
else method it doesn’t add any value, I can see in the log the value that fits the if method, but I think the problem is that I can’t call the execSQL method.
Near the bottom of the 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); ");
}
Code 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 {
}
}
});
}
Solution
FAVCampaignName contains single quotes, so the database considers strings to end in köy’ and does not know how to handle de….
'Starbucks's 2.kahve %50 indirimli'
Use \escape all single quotes or use parameterized queries.