Working with the SQLite-Database - Cursors

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Re: Working with the SQLite-Database - Cursors

Postby joemarieamparo » Wed Jan 26, 2011 7:18 am

Hello,

How about putting the DB in the path of your choice? I've been doing trial and error about using the path of the db where I put my DB which I created using SQLite Manager browser. I can't seem to do it right.

Can you help me please.

Thanks very much.
[url="http://mydailyservings.wordpress.com"]Joemarie Amparo[/url]
joemarieamparo
Freshman
Freshman
 
Posts: 9
Joined: Thu Jan 20, 2011 6:35 am
Location: Cebu, Philippines

Top

Re: Working with the SQLite-Database - Cursors

Postby mmarkeh » Fri Jan 28, 2011 12:27 am

Can someone point me in the right direction please, i'm getting error:

The method createDatabase(String, int, int, null) is undefined for the type calculate


could this possibly be related to using a more recent sdk version?

any help would be super :)


p.s. i think im being really stupid, but i can't find any link to download the source.
mmarkeh
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Jan 28, 2011 12:07 am

Re: Working with the SQLite-Database - Cursors

Postby bigassdragon » Wed Feb 02, 2011 5:34 pm

Hey I was wondering is there a way to get your android phone to be able to add information to a normal SQL database? I am curiouse because I would like to know how that is done.
bigassdragon
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Feb 02, 2011 5:29 pm

Re: Working with the SQLite-Database - Cursors

Postby dientrinh » Wed Feb 23, 2011 3:39 am

hi I got an error when I try to write a SQLite app. can you fix for me? Here is codes.
sqlite1.java file. Here is the main file.

Code: Select all
package org.com.sqlite1;


import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;

public class sqlite1 extends ListActivity {
   private DBhelper mDB;
   private Cursor mCursor;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mDB = new DBhelper(this);
        getData();
    }
   
    private void getData(){
       mCursor = mDB.getAllUsers();
       startManagingCursor(mCursor);
       String[] from = new String[]{DBhelper.nameCol};
       int[] to = new int[] {R.id.text1};
       SimpleCursorAdapter users = new SimpleCursorAdapter(this, R.layout.users_row, mCursor, from, to);
       setListAdapter(users);
   
    }
}


Here is code of BDHelper.java file.
Code: Select all
package org.com.sqlite1;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;



public class DBhelper extends SQLiteOpenHelper{



   public static final String TAG ="DBAdapter";
   
   private static final String DATABASE_NAME = "sqlite1";
      private static final int DATABASE_VERSION = 2;
      
      static final String hocsinhTable="hocsinh";
      static final String nameCol = "name";
      public static final String KEY_ID = "_id";
      

      
      SQLiteDatabase mDB= null;
      
      private static final String DATABASE_CREATE = "create table hocsinh (_id integer primary key autoincrement, "
         + "name text not null);";

      public DBhelper(Context context) {
         super(context, DATABASE_NAME, null, 2);
         // TODO Auto-generated constructor stub
      }
      

   @Override
   public void onCreate(SQLiteDatabase db) {
      // TODO Auto-generated method stub
   
      db.execSQL(DATABASE_CREATE);
      ContentValues cv = new ContentValues();
      cv.put(nameCol, "dien");
      mDB.insert(hocsinhTable, nameCol, cv);
      cv.put(nameCol, "hoai");
      mDB.insert(hocsinhTable, nameCol, cv);
      
   
      
   }

   @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      // TODO Auto-generated method stub
      db.execSQL("DROP TABLE IF EXISTS " + hocsinhTable);
         onCreate(db);
      
   }
   

   
   public boolean deleteUser(long rowId)
   {
      return mDB.delete(hocsinhTable, KEY_ID + "=" + rowId, null) >0;
   }
   
      
   public Cursor getAllUsers() {
      return mDB.query(hocsinhTable, new String[] {KEY_ID, nameCol}, null, null, null, null, null);
      
   }

}


Here is users_row file
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>


thanks
dientrinh
Freshman
Freshman
 
Posts: 3
Joined: Tue Dec 07, 2010 5:16 am

Re: Working with the SQLite-Database - Cursors

Postby rofridman » Wed Mar 16, 2011 8:23 pm

Hello android developers here i am again ... i helped myself with some android tutorials and i came this far so now i need somebody to help me get further because i am lost right now I don't know how to get mine data from sqlite database that i created and it hopefully works... when you are going to open the project there is a pregled.java file there are my attempts to get my data from sqlite database and in Vnos.java is the creating and inserting into database... i would realy appriciate your help . Thank you in advance!

here i added my project:
http://www22.zippyshare.com/v/71392424/file.html

ime_priimek means name firstname
vnos means input
pregled means overview

if there is something you don't understand just ask me...

Please help me i am not realy good at programming and i need this bad i am doeing my best but it is not enough i would just ask you if somebody could help me get my data from my database so it would be transfered into listview

i am desperet
rofridman
Freshman
Freshman
 
Posts: 8
Joined: Tue Jan 04, 2011 5:00 pm

Re: Working with the SQLite-Database - Cursors

Postby rofridman » Thu Mar 17, 2011 10:12 pm

Can somebody tell me why this code doesn't work for me ? please i am askin the same freaking question 2 months and nobody helps me i realy wanna learn but i need a bit of your help.

Code: Select all
import java.util.ArrayList;
import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Pregled extends Activity {

   
   private ListView lv1;
   protected static SQLiteDatabase db;
   private final String IME_TABELE = "PRIJATELJI";
   
   public ArrayList<ArrayList<Object>> getAllRowsAsArrays()
   {
      // create an ArrayList that will hold all of the data collected from
      // the database.
      ArrayList<ArrayList<Object>> dataArrays =
         new ArrayList<ArrayList<Object>>();
   
      // this is a database call that creates a "cursor" object.
      // the cursor object store the information collected from the
      // database and is used to iterate through the data.
      Cursor cursor;
   
      try
      {
         // ask the database object to create the cursor.
         cursor = db.query(
               IME_TABELE,
               new String[]{"ime_priimek"},
               null, null, null, null, null
         );
   
         // move the cursor's pointer to position zero.
         cursor.moveToFirst();
   
         // if there is data after the current cursor position, add it
         // to the ArrayList.
         if (!cursor.isAfterLast())
         {
            do
            {
               ArrayList<Object> dataList = new ArrayList<Object>();
   
               dataList.add(cursor.getString(0));
   
               dataArrays.add(dataList);
            }
            // move the cursor's pointer up one position.
            while (cursor.moveToNext());
         }
      }
      catch (SQLException e)
      {
         Log.e("DB Error", e.toString());
         e.printStackTrace();
      }
   
      // return the ArrayList that holds the data collected from
      // the database.
      return dataArrays;
   }
     
   
   @Override
   
   public void onCreate(Bundle icicle)
   {
   super.onCreate(icicle);
   setContentView(R.layout.pregled);
   lv1=(ListView)findViewById(R.id.ListView01);
   // By using setAdpater method in listview we an add string array in list.
   lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1));
   }
}
rofridman
Freshman
Freshman
 
Posts: 8
Joined: Tue Jan 04, 2011 5:00 pm

Top

Re: Working with the SQLite-Database - Cursors

Postby DMacATTACK » Sun Mar 20, 2011 10:35 pm

Mods please delete this post. I can't figure out how to.
Last edited by DMacATTACK on Sun Mar 20, 2011 10:38 pm, edited 2 times in total.
User avatar
DMacATTACK
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Thu Dec 31, 2009 6:31 pm
Location: Canada

Re: Working with the SQLite-Database - Cursors

Postby DMacATTACK » Sun Mar 20, 2011 10:36 pm

@rofridman:

I'm glad you found the right place for your question, but it seems you really don't understand databases.
The method you were trying to use:

cursor = db.query(
IME_TABELE,
new String[]{"ime_priimek"},
null, null, null, null, null
);

is incorrect and incomplete.

A database is like a "holder" for vast amounts of data that is stored with minimal space.
databases hold "tables" which are essentially organized charts of the data you want to use/store.
From a program you can insert, select, update, modify the data in these tables as you wish.

The cursor object lets you "read" data from the table by using a SELECT command.
Which relates to your problem: Your method only contains the table, and some string called "ime_priimek". If you look on the first page of this thread, the post made by plusminus you will see that he has used more than just a table and some random string. He used:

Cursor c = myDB.query("SELECT FirstName,Age" +
" FROM " + MY_DATABASE_TABLE
+ " WHERE Age > 10 LIMIT 7;",
null);

You can actually see the query "SELECT FirstName, Age FROM .... WHERE....":

Also you can see how the function is laid out here:

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

There are also other ways to execute the queries to acquire the data you wish. I personally use something like this:

String strQuery = "SELECT * FROM People WHERE Age >10";
db.rawQuery(strQuery,null);

It seems you are having a lot of problems with this. so please pick up a book. Or download it, Computer Science books are readily available online within months of publication.
User avatar
DMacATTACK
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Thu Dec 31, 2009 6:31 pm
Location: Canada

Re: Working with the SQLite-Database - Cursors

Postby meetjasper » Mon Mar 21, 2011 12:34 pm

hey Thanks for the tutorial...but i am getting error for createDatbase. DB_Project_Activity is my project name...

I have copied your zipped project and paste it in the Eclipse still no use.. see error description

The method createDatabase(String, int, int, null) is undefined for the type DB_Project_Activity
meetjasper
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Mar 21, 2011 12:21 pm

Re: Working with the SQLite-Database - Cursors

Postby gurnoorinder » Fri Mar 25, 2011 4:31 am

your example is wonderful... and u have explained it in a simple way and anyone with little SQL experience can understand it...

I want to ask you one thing, what if i want to show the results in this manner:


id name age


and then the rest of the data at the bottom.... ???
gurnoorinder
Freshman
Freshman
 
Posts: 9
Joined: Thu Mar 24, 2011 10:12 pm

Re: Working with the SQLite-Database - Cursors

Postby sorinachio » Sun Aug 28, 2011 4:16 pm

Hello. Good tutorial but I have a question. Where the database is stored??
sorinachio
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Aug 28, 2011 4:14 pm

Re: Working with the SQLite-Database - Cursors

Postby antonykkk » Tue Sep 20, 2011 7:04 am

antonykkk
Freshman
Freshman
 
Posts: 3
Joined: Tue Sep 20, 2011 7:01 am

Re: Working with the SQLite-Database - Cursors

Postby la3bna » Fri Oct 07, 2011 10:29 am

I got a question.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  @Override
  2.              protected void onListItemClick(ListView l, View v, int position, long id) {
  3.                 super.onListItemClick(l, v, position, id);
  4.               konto = (AccountDbAdapter.KEY_BODY);
  5.                      
  6.                 getPIN();
  7.                 message = "s " + konto + " " + pin;
  8.             sendSMS("92802386", message);
  9.                 Toast();
  10.             }
  11.             }
  12.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


I cant get the content of the BD entry..

I want it be read in the line : konto = (AccountDbAdapter.KEY_BODY);

The result is body . And not what I have entered in the DB..

Do I need to use this part of DBadapter?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public Cursor fetchNote(long rowId) throws SQLException {
  2.  
  3.         Cursor mCursor =
  4.  
  5.             mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
  6.                     KEY_TITLE, KEY_BODY}, KEY_ROWID + "=" + rowId, null,
  7.                     null, null, null, null);
  8.         if (mCursor != null) {
  9.             mCursor.moveToFirst();
  10.         }
  11.         return mCursor;
  12.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4



All is adapted from the notepad tutorial on developer.android.com
la3bna
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Wed Mar 30, 2011 5:49 am
Location: Stavern, Norway

Re: Working with the SQLite-Database - Cursors

Postby alvin.amigo » Mon Oct 10, 2011 1:31 am

Thanks for the great post.. I was thinking, can the SQLITE database inside the emulator be updated or edited using command prompt?
alvin.amigo
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Oct 10, 2011 1:28 am

Top
Previous

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests