when I try to request informations from my sqlite database I get this error:
07-19 02:17:10.835: E/AndroidRuntime(689): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.bodprod.dkr/de.bodprod.dkr.MediNotmediList}: java.lang.IllegalArgumentException: You must supply an SQL string
My Database Request:
public ArrayList<HashMap<String,Object>> getAllNotmedis(){
ArrayList<HashMap<String,Object>> notmediList = new ArrayList<HashMap<String,Object>>();
String selectQuery = "SELECT medi_id, medi_wirk, medi_handel FROM dkr_medi_liste ORDER BY medi_wirk";
Log.d("ALLNOTMEDIS",selectQuery);
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
HashMap<String , Object> notmedi_item;
if (cursor.moveToFirst()) {
do {
notmedi_item = new HashMap<String, Object>();
notmedi_item.put("sql_id", Integer.parseInt(cursor.getString(0)));
notmedi_item.put("wirk", cursor.getString(1));
notmedi_item.put("handel", unescape(cursor.getString(2)));
notmediList.add(notmedi_item);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return notmediList;
}
This line make the error:
SQLiteDatabase db = this.getWritableDatabase();
Why I become this error? The database exist and is filled up with data...
The error was in the onCreate section.
I tried to insert a blank line with db.execSQL.
I get the error on this line becaause SQLiteDatabase db = this.getWritableDatabase(); calls the onCreate method.
You should create Database Adapter that will serve as the connection between your app and your database
java.lang.IllegalArgumentException: You must supply an SQL string
Above exception occurs if you pass an invalid Sql Query statement in the rawQuery Method. There must be a problem in below statement:
= "SELECT medi_id, medi_wirk, medi_handel FROM dkr_medi_liste ORDER BY medi_wirk";
Related
I am getting an error with the method below.
I would like to do a SELECT WHERE statement.
My goal is to know, if the variable name is already in my database.
The error seems to occur at statement.execute(); It is :-
"Caused By : unknown error (code 0): Queries can be performed using SQLiteDatabase query or rawQuery methods only. "
The program has worked properly many times before but now it does not.
The method is :-
public int count(String name) {
SQLiteDatabase database = getWritableDatabase();
String countQuery = "SELECT * FROM myBD WHERE NAME1 = ?";
SQLiteStatement statement = database.compileStatement(countQuery);
statement.clearBindings();
statement.bindString(1, name);
statement.execute();
//database.close();
SQLiteDatabase db = this.getReadableDatabase();enter code here
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
database.close();
return count;
}
You could use the query convenience method e.g. :-
public int count(String name) {
SQLiteDatabase database = getWritableDatabase();
String whereclause = "NAME1=?";
String[] whereargs = new String[]{name};
Cursor cursor = database.query("myBD",null,whereclause,whereargs,null,null,null);
int count = cursor.getCount();
cursor.close();
database.close();
return count;
}
Note the above is in-principle code, it hasn't been checked or run and may therefore contain some errors.
Unknown errors are often related to subsequent lines following a ; the convenience methods enclose arguments and build the SQL on your behalf.
You can also use
database.rawQuery("Your query here");
I try to call a method and write to my database from another activity in android but my app crashes because it cannot write. My activity does not have On Create because it does not need it. Therefore I have this code:
DB db = new DB(ServSearcher.this); which I want to give me acess to my Database and then I use my method below like:
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
db.addContact(new Contact(var, getTime()));
Here is my logcat error:
java.lang.NullPointerException
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:268)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.android.SecondActivity.DB.getAllContacts(DB.java:88)
at com.android.SecondActivity.ServSearcher$5.onAvailable(ServSearcher.java:189)
I think it is obvious that my app cannot write to database so this line is wrong DB db = new DB(ServSearcher.this); or at least does not gives me access, any ideas?
edit after V_J comment:
Here is my method on DB
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setTime(cursor.getInt(0));
contact.setName(cursor.getString(1));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
I think you can solve your problem with the singleton pattern, I leave a link below to your explanation.
https://en.wikipedia.org/wiki/Singleton_pattern
In my activity I have a spinner with data taken from SQLite db by this query:
private List<String> ottieniAnni(){
List<String> result = new LinkedList<String>();
SQLiteDatabase db = new BHelper(this).getReadableDatabase();
String sql = "SELECT DISTINCT strftime('%Y',"+GTable.DATE+") FROM "+GTable.TABLE_NAME;
Cursor c = db.rawQuery(sql, null);
while (c.moveToNext()){
result.add(c.getString(0));
}
db.close();
return result;
}
Now through button, I want to delete all records in the year chosen by the user. I used the method db.delete (), but so will delete all the records.
Should I set a parameter but do not know how to do it:
SQLiteDatabase db= mHelper.getWritableDatabase();
db.delete(GiornateTable.TABLE_NAME, null, null );
db.close();
Try like
SQLiteDatabase db= mHelper.getWritableDatabase();
int rowcount = db.delete(GiornateTable.TABLE_NAME,
"GIVE YOUR COLUMN NAME" + "=?",
new String[] { "GIVE YOUR VALUE" });
Log.d("No of record deleted",String.valueOf(rowcount));
you can use the same syntax as you do with the select, only for deleting
I created and populated a SQLite database with the Firefox SQLite Manager. I copied the .sqlite file into the assets folder of my Android project.
My aim is to use this records to populate a spinner. When I query the database, the cursor displays the columns but no records.
My DatabaseHandler class extends SQLiteOpenHelper and contains methods for onCreate, onUpgrade and getAllOffices.
public List<String> getAllOffices(){
List<String> offices = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT OfficeId as _id, OfficeName FROM " + TABLE_OFFICE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
offices.add(cursor.getString(1));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning offices
return offices;
}
Any ideas on why the records are not returned would be appreciated.
Try this:
public List<String> getAllOffices(){
List<String> offices = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT OfficeId as _id, OfficeName FROM " + TABLE_OFFICE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
offices.add(cursor.getString(cursor.getColumnIndex("_id")));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning offices
return offices;
}
Basically it will just ensure that you are using the correct column index. Otherwise if you are using eclipse then try the cellobject plugin to browse the database and make sure your table is populated as expected:
http://marketplace.eclipse.org/content/cellobject-sqlite-xml-browser#.UWOU5RaBD3g
Please try this and may be useful for you.
public List<String> getAllOffices(){
List<String> offices = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_OFFICE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
System.out.println("CURSOR SIZE:"+cursor.getCount())
// looping through all rows and adding to list
if(cursor.getCount()!=0){
if (cursor.moveToFirst()) {
do {
System.out.println(cursor.getString(c.getColumnIndex("as per your db column name"));
} while (cursor.moveToNext());
}
}
// closing connection
cursor.close();
db.close();
// returning offices
return offices;
}
First of all you have to just see console output if you are getting right then add into your office bean
I have created a database. And opened it :
public SQLiteDatabase open() {
String path = "/data/data/com.develop.assetcapture/databases/Asset_Directory";
db = SQLiteDatabase.openOrCreateDatabase(path, null);
return db;
}
I'm writing a user registration and what to enable them to reset their passwords.
I have no problem inserting new records or querying the database. However on my resetPassword(username,password) method I get an error message:
android.database.sqlite.DatabaseObjectNotClosedException:
Application did not close the cursor or database object that was opened here
Please help, I've been stuck on this for too long now.
When you query the database you usually receive a Cursor object. Here is an example:
public Cursor querySomething() {
final String whereClause = ...
final String[] params = ...
final Cursor c = this.getReadableDatabase().query(
Table.TABLE_NAME,
Table.allColumnNames(),
whereClause,
params, // parameters for where clause
null, // group
null, // having
null); // order
return c;
}
Using the cursor c you access the rows in the answer. When you have finished processing the answer you have to close the cursor object:
c.close();