I'm back with another question regarding Android databases.
I'm storing a simple string into my android database and then wish to display all the entries as a list. However, the list shows nothing on my emulator (or my actual device) and neither does LogCat (either when saving entries into the DB or when calling them).
Below you have my code
the save entries function:
public long createEntry(String entry){
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ENTRY, entry);
return db.insert(DATABASE_TABLE, null, initialValues);
}
the get entries function:
public Cursor fetchAllEntries(){
return db.query(DATABASE_TABLE,new String [] {KEY_ROW_ID, KEY_ENTRY}, null, null, null, null, null);
}
the List Activity:
entryList = (ListView)findViewById(R.id.recordsList);
Cursor c = dbAdapter.fetchAllEntries();
if(c!=null){
startManagingCursor(c);
String [] from = new String [] {PirelliDbAdapter.KEY_ENTRY};
int [] to = new int [] {R.id.text1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.entryrow, c, from, to);
entryList.setAdapter(adapter);
}
dbAdapter.close();
And finally the List Activity's XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/recordsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#+id/textViewList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Thanks a lot for taking the time to look over this and help me out. I'm really stumped and don't know where to go from here...
Firstly, check that your database is created or not then check whether table is created or not , through command line. If all-things are OK then try to print cursor to check it retrieves data from db or not. for printing the cursor values use following logic..
*if(!cursor.isAfterLast())
{
cursor.moveToFirst();
do{
String entry = cursor.getString(cursor.getColumnIndex(KEY_ENTRY));
Log.v("value:",entry);
cursor.moveToNext();
}while(!cursor.isAfterLast());*
Your code has no issue , will you please check in the database file weather the data is there one way is use sqlite browser (or) go through this in command prompt
{$ adb shell $ cd data/data/projectname/databases/
check if your db file is created and then move on to -->
sqlite3 database file name you will find the sqlite prompt- hence you can explore as .tables and select * from table Name }
the above steps are mentioned in view of newbees if you know this earlier please ignore
to explore the db file.
please check this https://stackoverflow.com/questions/2149438/tool-to-see-android-database-tables-and-data
Related
So I created first database, and now I need to create separate database, zipped it and send it to server.
I'm just wondering if it is doable?
And how can I do it?
Here's the basic logic of what you want to do, it'll have to be supplemented with specific code from here: https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
and here: https://developer.android.com/reference/android/database/Cursor.html
and here: https://developer.android.com/reference/android/content/ContentValues.html
SQLiteDatabase database1 = SQLiteDatabase.openDatabase(<PATH TO DATABASE1>, null, 0);
SQLiteDatabase database2 = SQLiteDatabase.openOrCreateDatabase(<WHEREVER YOU WANT DATABASE2 TO GO>, null);
Cursor cursor = database1.query(<You'll really just have to fill this in with what you're pulling from database1, it's way too specific>);
ContentValues contentValues;
while (cursor.moveToNext()) {
contentValues = new ContentValues();
contentValues.put(<Column key>, <Column value from cursor>)
// I.E. contentValues.put(ID_KEY, cursor.getString(cursor.getColumnIndex(ID_KEY));
// <INSERT THE REST OF YOUR COLUMNS INTO contentValues>
database2.insert(<TABLE_NAME>, null, contentValues);
}
cursor.close();
database1.close();
database2.close();
To send it, that depends on way too many things for me to address. You have the database file now, send it using whatever protocol you please. If you don't know how to do this, search for "Android send File to Server" (try with an without quotes).
I am fairly new to Android and trying to learn how things work module by module. Here's what I am trying to do:
Show a word with a favorite checkbox (image). If a user taps on it then the database is updated and a column in database table stores its value (1 for checked, 0 for unchecked). I am using a cursor to retrieve values of both the word and favorite checkbox. Tapping on the favorite image correctly updates the database without any problem.
The problem I am facing is:
Unless I exit the application and start it again, the cursor doesn't fetch the recent changes made to the database. To explain it further, when I navigate to the next/previous word (using a button at the bottom of screen) the values retrieved aren't the latest ones i.e it seems like the cursor still has the old database values and not the updated ones.
I did search through Google, StackOverflow to get a concrete solution but it seems like I am not using the right search terms. I know this has something to do with updating cursor and the fact that requery is depreciated but again I have lost direction.
[EDIT] Using the below mentioned method to get Cursor:
public Cursor getWords() {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqlSelect = {"_id", "word", "favourite"};
String sqlTables = "word_list";
qb.setTables(sqlTables);
Cursor c = qb.query(db, sqlSelect, null, null,
null, null, null);
c.moveToFirst();
return c;
}
This method is called when user taps on favorite image to update the database:
public void setFavWord(int markFav, int wordPos) {
SQLiteDatabase db = getWritableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
ContentValues values = new ContentValues();
values.put("favourite", markFav);
db.update("word_list", values, "_id = " + wordPos, null);
}
A cursor is not dynamic; it shows a snapshot of the database at the time the query was executed.
When the database changes, you must execute the query again.
As suggested by #Sreekanth in comments section, I am updating the cursor whenever favorite image is tapped. Although it is working just fine but I think it as a workaround rather than a solution. Maybe I am wrong in saying so.
Hello I would like a detailed explanation or an example of retrieving two details in the android contact list(name and Number). Once I receive these details I want to write them onto a XML file. As I need to send this XML file to a php server.
I know the permission required to do this is
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
and I need to work with Android Cursor and ContactsContract to do this. But am not being able to find a good example to do the same. If any one could provide a good pointer or a detailed example of what am looking for it will be highly appreciated. Thanks in advance.
private void readContacts() {
String[] projection = {ContactsContract.Contacts.DISPLAY_NAME};
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, projection, null, null, null);
Log.i("Demo", "" + cursor.getCount());
String[] strs = new String[cursor.getCount()];
cursor.moveToFirst();
int i = 0;
do {
strs[0] = cursor.getString(0);
}while (cursor.moveToNext());
ArrayAdapter<String> liststr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
getListView().setAdapter(liststr);
}
}
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
I am fetching the list of tables from the database. I am using this code to get the list of tables:
public void showAllTable()
{
db.execSQL("select name from sqlite_master where type = 'table'");
}
This query execute successful in to the shell window.
Now I want to display that list of tables in Android. How it is possible? I am calling this function from another activity.
Edited:
Thanks to Stack Overflow, I found the answer here.
The usual way to display a list of data is to use a ListView. To do this you will need to do the following:
1: change your db query to db.query instead of db.exec and store the values in a List:
ArrayList<String> tables = new ArrayList<String>();
Cursor mCursor = db.query("sqlite_master", new String[]{"name"}, "type = table",
null,null,null,null);
if (mCursor.getCount() > 0) {
for (mCursor.moveToFirst(), !mCursor.isAfterLast(), mCursor.moveToNext()) {
tables.add(mCursor.getString(0));
}
}
/** Important! always close cursors and DB's */
mCursor.close();
db.close();
return tables;
2: Use a ListView and an ArrayAdapter to display the information. See The Android Tutorial on how to do this.
How can fetch values form database and display it in a textview in android?
Good to see you've given it some thought and tried on your own.
http://developer.android.com/guide/topics/data/data-storage.html#db has some good info on using SQLite on Android
It's also used in the Notepad tutorial: http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html
I personally learned by using part of the guide from the "Hello, Android" book. The source code is available at: http://www.pragprog.com/titles/eband3/source_code - the SQL example is the one called 'Eventsv1'
You need SQLiteOpenHelper class to fetch readable instance of SQLiteDatabase in android. Then using query method you can get Cursor object of you query.
Can you explain more about what you want to do?
Try this,
Cursor c = db.query(TABLE_NAME, columns, null, null, null, null, null);
Then write
if(c!=null) {
c.moveToFirst();
while(!c.isAfterLast()) {
String col1Value = c.getString(1);//here you get col1 value
String col2Value = c.getString(2);//here you get col2 value
c.moveToNext();
}
c.deactivate();
c.close();
}