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'"
Related
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
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();
Mistakenly I have put a double value divided by zero in an SQLite database column.
In "DB Browser", it looks like string "Inf" or "inf". However, "WHERE MY_COLUMN = 'inf'" gives no result.
How can I retrieve all records containing this value via an SQL query?
Also, how can I identify this value in the cursor loop?
I have tried these:
cursor.getString(cursor.getColumnIndex(MY_COLUMN)).equalsIgnoreCase("inf")
cursor.getDouble(cursor.getColumnIndex(MY_COLUMN)) == Double.POSITIVE_INFINITY
No result as well.
Table -article-
id |nam|image|id_categor(FK)|
table category
id |categorieName|
I have two tables in the database. The first table has a foreign key to the second table. Can I get name from the second table by the foreign key ?
need a JSON like
"categories":[
{"categorieName":"test1",
"data":[
{
"name":"wallpaper1",
"image":"tv_101.jpg"
},
{
"name":"wallpaper2",
"image":"tv_102.jpg"
}
]
Welcome, You need to provide a better description with a better code so everyone can understand easily.
If I understand your problem properly then you need a join. You can use SQLite rawQuery to get the desired output.
For Example:
private final String JOIN_QUERY = "SELECT * FROM article a INNER JOIN categories b ON a.id=b.other_id WHERE b.property_id=?";
db.rawQuery(MY_QUERY, new String[]{String.valueOf(propertyId)});
EDIT: I still don't get your question but I think you are using PHP
First of all you cannot directly get the JSON from the database. What you need to do is:
Query your required data using PHP
SELECT * FROM categories then loop it in php and get all articles in each category by querying SELECT * FROM articles WHERE cate_id=loopCategoy_Id
And save it in a key value array
Then convert the array into JSON
Send JSON response
Did you try join ?
Joining two tables by common columns will definitely help here
I successfully attached DB to another DB using SQLCipher like this:
ATTACH DATABASE '/storage/emulated/0/Android/data/testApp/files/dbTest1.s3db'
AS dbTest1 KEY 'Password';
I am not getting any error. When I try to use this query:
SELECT column1, column2, column3 FROM dbTest1.SaTTest
UNION SELECT column1,column2,column3 FROM SatTest
WHERE (Address1 LIKE '%cor%' OR column1 LIKE '%cor%')
I am getting this error:
I/Database(2587): sqlite returned:
error code = 1, msg = no such table: dbTest1.SaTTest.
How can i use alias in attached database tables?
I believe it's just a typo, it should be dbTest1.SatTest instead of dbTest1.SaTTest. Please always keep in mind case-sensivity