In the below given program Iam inserting imei_number in the table .However I have to retrieve the inserted data and display it in different method.Please guide.thanks in advance
onCreate()
{
try {
imeiDB = this.openOrCreateDatabase(IMEI_DB_NAME, MODE_PRIVATE, null);
imeiDB.execSQL("CREATE TABLE IF NOT EXISTS " +
IMEI_TABLE_NAME +" (IMEI_number_db VARCHAR);");
imeiDB.execSQL("INSERT INTO " + IMEI_TABLE_NAME +" Values ("+IMEI_number_db+");");
Cursor c = imeiDB.rawQuery("SELECT IMEI_number_db FROM " + IMEI_TABLE_NAME , null);
if (c != null ) {
if (c.moveToFirst()) {
do {
IMEI_number = c.getString(c.getColumnIndex("IMEI_number_db"));
}while (c.moveToNext());
}
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
if (imeiDB != null)
imeiDB.execSQL("DELETE FROM " + IMEI_TABLE_NAME);
imeiDB.close();
}
}
/****************/
fetch the data in another method
private collectdata()
{
String IMEI_number ="";
Cursor c = imeiDB.rawQuery("SELECT IMEI_number_db FROM " + IMEI_TABLE_NAME , null);
if (c != null ) {
if (c.moveToFirst()) {
do {
IMEI_number = c.getString(c.getColumnIndexOrThrow("IMEI_number_db"));
}while (c.moveToNext());
}
}
}
The only one column do you search in the query is IMEI_number_db, so the cursor only has one column. You should change this:
IMEI_number = c.getString(c.getColumnIndex("IMEI_number_db"));
For this:
IMEI_number = c.getString(0);
You can also try the following code.
IMEI_number = c.getString(c.getColumnIndexOrThrow("IMEI_number_db"));
Related
I have a simple SQLite database with a table having 2 columns, I just fetch data from the table using:
select * from myprofile;
This code works in pre 5.0 android properly, but doesnt work on 5.0 or 5.1 android.
Are there any changes to the normal select statement. I checked their documentation but couldnt find anything related to this
public void chkFirstTime()
{
SQLiteDatabase sampleDB1 = null;
Cursor c = null;
try {
sampleDB1 = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
sampleDB1.execSQL("CREATE TABLE IF NOT EXISTS myprofile (username Text,useremail Text);" );
Log.w("table created", "table created");
c = sampleDB1.rawQuery("SELECT username,useremail FROM myprofile", null);
count= 0;
if (c != null ) {
if (c.moveToFirst()) {
do {
username1 = c.getString(c.getColumnIndex("username"));
Email1 = c.getString(c.getColumnIndex("useremail"));
count = count + 1;
}while (c.moveToNext());
}
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database1 in check_if_exists");
} finally {
if (sampleDB1 != null)
sampleDB1.close();
}
}
I built a table in the database, and now I want to access the all of the values in a certain column. Finally, I want to put the data into byte[].
Part of my code:
db.execSQL("create table thing(id integer primary key" +
" autoincrement, name varchar(20))");
List<Integer> all = new ArrayList<Integer>();
String sql = " SELECT id from " + DB_NAME;
Cursor result = this.db.rawQuery(sql, null);
for (result.moveToFirst(); result.isAfterLast(); result.moveToNext()) {
all.add(result.getInt(0));
}
String[] fstr = (String[]) all.toArray();
for (String bstr : fstr) {
byte[] bbs = bstr.getBytes();
}
Use this code
String[] fstr = new String[result.getCount()] ;
do {
int posi = result.getPosition();
fstr[posi] =result.getString(0);
}while (result.moveToNext());
try this
Cursor c=this.db.rawQuery(sql, null);
if(c.getCount()>0)
{
for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
{
String str=c.getInt(0));// or c.getString();
}
}
else
{
Log.d(" Null value in cursor ", " null ");
}
c.close();
dbhelper.close();
I want to get a row and this row have 2 columns which named username and password my code is here:
String selection = PROFILE_COLUMN_USERNAME + " = '" + userName+ "' AND " +PROFILE_COLUMN_PASSWORD + " = '" + password + "'";
Cursor cursor = database.query(TABLE_NAME, new String[] {PROFILE_COLUMN_USERNAME, PROFILE_COLUMN_PASSWORD }, selection,null, null, null, null);
I got a sample data like username = erdem and password= c on my db but when i write this sample and write to get username like this:
String s =cursor.getString(cursor.getColumnIndex(PROFILE_COLUMN_USERNAME));
it doesn't work. Why?
Try something like below : Here "path_on_server" is the name of the column in the DB table whose value is being extracted. You can change it to your table's column name.
String query = "some query";
Cursor c = newDB.rawQuery(query, null);
if (c != null) {
if (c.moveToFirst()) {
do {
mPathOnServer = c.getString(c
.getColumnIndex("path_on_server"));
} while (c.moveToNext());
}
}
} catch (SQLiteException se) {
Log.e(getClass().getSimpleName(),
"Could not extract information from DB");
}
Use moveToFirst, read the documentation.
Try this way...
Cursor cursor = null;
try {
String queryString = "SELECT * FROM Table_Name";
cursor = myDatabase.rawQuery(queryString, null);
if (cursor != null && cursor.moveToFirst()) {
do {
String nextUser = new String(cursor.getString(cursor
.getColumnIndex("driver_fname")));
} while (cursor.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.deactivate();
cursor.close();
cursor = null;
}
if (myDatabase != null) {
myDatabase.close();
}
}
I have a simple code that manages to successfully query an SQLite Database and convert that result from cursor to string in order to display it on screen.
My problem now would be invalid queries that make the App Crash. Would there be a way to successfully handle invalid queries? Preferably something that would keep my app from crashing and would just redirect the user to the home page and display a toast of warning.
So far my method for searching looks like this:
public String search(DataBaseHelper myDB){
SQLiteDatabase db = myDB.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT BuildingColor, Room FROM LSBuildingsDB WHERE _id =" + newString, null);
cursor.moveToFirst();
String data = cursor.getString(cursor.getColumnIndexOrThrow("BuildingColor")) + " " +
cursor.getString(cursor.getColumnIndex("Room"));
//Toast msg = Toast.makeText(getBaseContext(),data, Toast.LENGTH_SHORT);
//msg.show();
cursor.close();
return data;
}
Cursor cursor = NULL ;
try
{
cursor = db.rawQuery("SELECT BuildingColor, Room FROM LSBuildingsDB WHERE _id =" + newString, null);
if(cursor != NULL)
{
try {
if (cursor.moveToNext()) {
String data = cursor.getString(cursor.getColumnIndexOrThrow("BuildingColor")) +
" " + cursor.getString(cursor.getColumnIndex("Room"));
} else {
// Query result was empty, deal with it here.
}
} finally {
// Cursors should be closed
cursor.close();
}
}
}
catch (SQLiteException e) // (Exception e) catch-all:s are bad mmkay.
{
//print exception
}
Cursor cursor = null;
String data = "";
try
{
cursor = db.rawQuery("SELECT BuildingColor, Room FROM LSBuildingsDB WHERE _id =" + newString, null);
}catch (Exception e) {
// TODO: handle exception
}
I have a query like this
SELECT value1 FROM mytable where (value1 > value2) AND category = 1 ;
This is not executing properly in Android. Although if i manually go to
/data/data/packagename/databases/mydatabase.db
Here is the Code
try {
SQLiteDatabase sqdb = _context.openOrCreateDatabase(_DB_NAME, 0,
null);
String sql = null;
sql = "SELECT value1 FROM mytable where (value1 > value2) AND category = " + category
+"; ";
Cursor c = sqdb.rawQuery(sql, null);
Log.i("sql = ", sql);
if (c.moveToFirst()) {
while (!c.isAfterLast()) {
categoryValue = c.getString(0);
c.moveToNext();
}
}
Log.i("categoryValue", categoryValue);
sqdb.close();
c.close();
} catch (Exception e) {
e.printStackTrace();
}
And then execute it. Then it works perfectly fine.
What is happeneing?
i believe first you need to close cursor and then the database connection
//the order should be
c.close();
sqdb.close();