sql lite dynamic Query for sqllite-pcl xamarin forms - android

i have created dynamic tables in Xamarin forms by using SQLite queries
string cmdStr="CREATE TABLE Mytable ....";
i am able to create table using
SQLiteCommand command = new SQLiteCommand(sqlitConnection);
command.CommandText = cmdStr;
command.ExecuteNonQuery();
the above code is working fine ,i am able to save data to sql lite db
i am unable to get data from db using command.ExecuteQuery() using command string as "select * from Mytable". as seen in screenshot i am unable to fetch content from dynamic tables,please suggest any alternatives
enter image description here

The error message tells you exactly what is wrong - you are not specifying a Type argument T. The correct syntax is
List<T> results = command.ExecuteQuery<T>();
where T is the name of the class you are mapping the results to

Related

How to fix empty string while fetching it from Database?

I am fetching a string from Database using the column id. When I enter a query in SQLite DB Browser it returns what is need but the same query returns nothing when coded through Java.
My Data Base contains a table named drugs which has 3 columns i.e. drug_id, drug_name and drug_overview. Using drug_id i am fetching drug_overview. I have tried the query in db browser which returns me the correct string from drug_overview but the same query returns nothing when coded through java.
SQLite DB Browser query:
SELECT * FROM drugs Where drug_id = 50;
JAVA CODE:
String query105 = "SELECT * FROM drugs Where drug_id = " + drug_id;
Log.e("TESTDB1","Drugs table query: " + query105);
Cursor c105 = db.rawQuery(query105,null);
if (c105 != null){
while (c105.moveToNext()){
String overview = c105.getString(c105.getColumnIndexOrThrow("drug_overview"));
Log.e("TESTDB1","Overview: " + overview);
}
c105.close();
}
Expected result is Overview: Acyclovir is an antiviral drug. It slows the growth and spread of the herpes virus in the body. It will not cure herpes, but it can lessen the symptoms of the infection.Acyclovir is used to treat infections caused by herpes viruses, such as genital herpes, cold sores, shingles, and chicken pox, as well as varicella (chickenpox), and cytomegalovirus.Acyclovir may also be used for purposes not listed in this medication guide.
But the actual result is Overview:
empty
. When i change the id in my query it gives the correct result from a different drug.
I am afraid that your problem may be with the actual data itself as Mike said in comment, I think your database in the files is old and you haven't copied the latest to folder. Try to re-install and delete old database
Your query returns 0 or 1 lines so I think you should use c105.moveToFirst() instead of c105.moveToNext(). moveToNext is supposed to be used for a list, not for a single entry. Do something like:
if (c105.moveToFirst()){
String overview = c105.getString(c105.getColumnIndex("drug_overview"));
// do something with the result
}
c105.close();

Android: Retrive data from database for specific value

I am making an Android app that get the data from URL stores it in to the database (I am done till this stage) and use some of the field of that table.
I tried to fetch the specific data using this SQL query
"select persons_name where persons_name=s"
but there was an error
I/Database(23722): sqlite returned: error code = 1, msg = near "FROM": syntax error
you have to specify in which table the data is located.
The correct SQL is:
select persons_name FROM Persons where persons_name=s
assuming the table is Persons
This will be select all names who have name starting letter is 's'
"SELECT persons_name FROM persons WHERE persons_name LIKE 's%'
As pointed out by #JesusS, try this:
"SELECT persons_name FROM persons WHERE persons_name LIKE 's'"

Issue in android sqlite rawQuery with ORDER BY?

I am trying to get data in an order by executing rawQuery with join conditions. but i am unable to get expected result.
below is my query.
String queryString = "SELECT DISTINCT B.MakeID , B.Make FROM MakeList B JOIN BodyStyle BS ON B.Makeid = BS.makeid AND BS.Year = 2012 ORDER BY B.Make ASC";
First thing is no need to write ASC coz it is bydefault ascending order to display result so write simple ORDER BY B.Make and second thing specify what you need in results and post database fields also.
Verify that your databases match. You may have db data that your SQLite Manager is accessing that may be different that what is on your Android device. That is probably causing your unexpected behavior.

Titanium.DB read Blob from SQLite

I'm developing with SDK 1.6.2
I execute a query using the following SQL
var dbrows = db.execute('select item_logo , item_id,item_title,item_content from item');
I successfully get the values for item_id , item_title & item_content. However Ti.API.Info(getFieldByName('item_logo') returns a null value. I have the blob data in SQLLite ITEM table and it is a valid jpeg picture.
Is there any specific processing to be done at Titanium End (Code) to read the Blob data in a loop? Can't find any specific info # Titanium API docs.
Thanks
Vishnoo
you can do this, but the recommended approach is to store the blob as a file and keep the native file path in the database.
See this response from Appcelerator
http://developer.appcelerator.com/question/97041/imageview-sqlite-blob-example-not-working

call sql from another class for android app

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.

Categories

Resources