I am making a quiz app in which i have to select the number of question depending upon user choice.Actually i want to something like this
db.execute('select * from Quiz limit **Variable**')//Here i want to use variable instead of constant value.
Could you help me in this
Thanks in advance
Change your code as:
public void selectall(String variable){
Cursor c=db.rawQuery("SELECT from Quiz limit "+variable);
}
where variable is set according to user selection
NOTE : db.execute is not SQLiteDatabase class method in Android you will need to use execSQL or query methods for quering database table
and see this tutorial for performing all select,delete,update and insert operation on database in android :
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
Why not use something like this:
int num = 10;
db.execSQL("select * from Quiz limit " + num)
Related
This question already has answers here:
difference between rawquery and execSQL in android sqlite database
(2 answers)
Closed 6 years ago.
I have a databaseHelper class. But, I want to multiply two values of each row and store it in another column while a button is clicked. I have checked my query and it works fine but I want to directly multiply the values and make change in the database when a button is click. I don't want to return any values but simply execute the query.
What I did for this is I have created a method in my database helper class. This is the method:
public void getTicketTotal(){
SQLiteDatabase db=this.getWritableDatabase();
db.rawQuery("update ticket_table set total=quantity*item_price ",null);
}
and in the button click event I did this. But, it didn't multiply and store the values in the table:
myDb=new DatabaseHelper(TicketActivity.this);
myDb.getTicketTotal();
What to do?
The problem was with the method that I was using. The solution is to use db.execSQL instead of rawQuery. So the line of code would be:
public void getTicketTotal(){
SQLiteDatabase db=this.getWritableDatabase();
db.execSQL("update ticket_table set total=quantity*item_price ");
}
and simply call it from within the activity.
your update query is wrong. on which row you have to update total?
hope it helps
public void getTicketTotal(){
SQLiteDatabase db=this.getWritableDatabase();
db.rawQuery("update ticket_table tt set total= tt.quantity * tt.item_price" ,null);
}
I am missing something with all of the android SQLite tutorials.
I want to create a SQLite database that holds the autoincrementing key, and four text fields that I will pass in
I intend to pass in this SQL database
private static final String INSERT = "insert into " + TABLE_NAME + "(field1) values (?)" + "(field2) values (?)" + "(field3) values (?)";
but I'm not sure if the android sdk has a proper insert function.
I'm not sure how "Cursor" relates to anything I am trying to do, and I'm not sure how much object oriented initializing I should be trying to as opposed to just calling some built in android sdk functions.
insight appreciated, but please break it down
Will recommend you to go through NotePad exercise here is a link. Specially go through Exercise 1.
And for more depth knowledge you can go through project which I have created. Here is a link
Look for creatFeed function call here. I think will help to answer your problem.
I am trying to build a query in SQLite in Android. Query is very simple and looks like this:
SELECT
ifnull(object.icon_large, ifnull( object.image, item.IMG_DEF_COVER))
FROM
object , item
WHERE
object.id_item = 1 AND item.id_item = 1;
I've tried to use SQLiteDatabase.query(), but it can be used only for one table. Any suggestions how to combine queries or how to join them, using Android SQLiteDatabases query's syntax?
Thanks in advance!!!
#fox you can use rawquery() instead of query example:
SQLiteDatabase mydatabase;
mydatabase.rawquery("select * from a, b where a.id=b.someid", null);
or can use querybuilder's setTable() method to specify the joins example
builder.setTables(TABLEA+" LEFT JOIN "+TABLEB+" ON "+TABLEB+"."+KEY_ID+"="+TABLEA+"."+KEY_B_ID);
SELECT
ifnull(object.icon_large, ifnull(object.image, item.IMG_DEF_COVER))
FROM
object
JOIN
item
ON
object.id_item = item.id_item
WHERE
object.id_item = 1;
SELECT syntax
Hi
I have created a database for my application and i have added items to the database using methods from the database class. I am encountering a problem do when i try to execute a sql query in the other class(app.java), i need to reference a database and thats where im having the problem!
this is the sql query im trying to execute(in database.java)
public void getData(SQLiteDatabase db, String data){
String sql =
"SELECT permissions FROM genres WHERE name = "+data+";";
db.execSQL(sql);
}
and this is how i am calling it(in app.java)
appData.getData(db, chosenGenre);
I just dont no what to put for the "db" part in appData.getPermissions(db, chosenGenre);
Does anyone know how to do this?
Thanks
You would typically use a SQLiteOpenHelper to create the database files (if necessary) and obtain a SQLiteDatabase object which is used to access the actual database (files on disk read by SQLite).
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
Here's a nice tutorial
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/2/
You don't show the code where you define the database manager 'db'! If you are using a database created outside the android device, you might get some help from this link:
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
Also, I have found that it's best to frame all table names and supplied values in single quotes a la -
String sql = "SELECT permissions FROM 'genres' WHERE name = '" + data + "'";
Note that the semicolon is not required.
I am new to Android App development and was hoping if someone could help me with the sqlite query to search an item.
I want to take a string from the Edittext box and then use that string to search my sqlite database to display the entries back on the android screen. How can I do this?
For example if I wanted to find-
select * from table_name where name= str1; (here str1 is the string from edittext box).
How do I write the java code for this so that it displays all the information regarding str1 from the database to the android screen?
I appreciate your help
:-) The general way to go about it is to use something called Cursor. In the following code, db is the database.
Cursor cur = db.rawQuery("SELECT * FROM "+ table_name + ";");
You'll have to modify it to use your query instead. The next step is to get the values from the Cursor. textview1 and so on are TextView's in your UI. I'll assume you know how to do this, but if you need help with this please let me know. nameOfColumn is the name of the columns in your database (I think that's name in your case)
if(cur.moveToFirst()) {
if(cur != null){
//Display the text in TextView's
textview1.setText(cur.getString(cur.getColumnIndex("nameOfColumn"));
textview2.setText(cur.getString(cur.getColumnIndex("nameOfColumn2"));
}
}
Hope this helps! :-)