Android SQLite Example [closed]

bawltherapistΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 3 χρόνια και 6 μήνες)

90 εμφανίσεις



log in


careers 2.0


chat


meta


about


faq
Questions

Tags

Users

Badges

Unanswered

Ask Question

Android SQLite Example [closed]

2

1

android

sqlite

sqliteopenhelper


I am an new aspirant in ANDROID..

I have to create an application where I need to use SQLite Database.
I am not so well equipped with the
SQLite.

Kindly please let me know what the purpose and use of HELPER Class, and provide a simple database
creation and insertion example

Regards :)

share
|
improve this question

asked

Aug 18 '12 at 4:02


Rushabh Chheda

13
1
3

feedback


sqlite
-
tutorial


 
Lucifer

Aug
18
'
12
at
4:10

closed
as not a real question by
Chris Dennett
,
Tyler Treat
,
Reno
,
nandeesh
,
Graviton

Sep 5 '12 at 4:35

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or
rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be
reopened,
see the FAQ
.

4 Answers
active
oldest
votes
3

Sqlite helper class helps us to manage database creation and version management.
SQLiteOpenHelper
takes care of all database management activities. To use it,


1.Override
onCreate(), onUpgrade()
methods of
SQLiteOpenHelper
. Optionally override
onOpen() method.


2.Use this subclass to create either a readable or writable database and use the SQLiteDatabase's four
API methods
insert(), execSQL(), update(), delete()
to create, read, update and delete
rows of your table.

Example to create a MyEmployees table and to select and insert records:

public class MyDatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "DBName";

private static final int DATABASE_VERSION = 2;

// Database creation sql statement

private static final String DATABASE_CREATE = "create table MyEmployees

( _id integer primary key,name text not null);";

public DatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);

tagged

android
 
×
 
287785

sqlite
 
×
 
18607

sqliteopenhelper
 
×
 
146

Community Bulletin


Linked

Save selected news articles for
offline use


How I get Data from th SQLite
Database to a EditText?


Related

Android does not update SQLite
database


Android AsyncTask and SQLite DB
instance


Upgrade SQLiteDatabase Android


Android: cannot see app name in
data\data


How do i upgrade my pre
-
populated
sqlite database on my device
without re
-
creating tables?


Android SQLite code doesn't work
after switching from Mac OSX to
windows 7


The beginning to create DB using
the SQLiteOpenHelper in Android


upgrading SQLite DB in Android
using ContentProvider


Create Sqlite database with 8000
records


Android SQLite
DatabaseObjectNotClosedException


Concurrent writing to android
database (from multiply services)?


My Android SQLite DB query doesn't
return newly inserted data until I
exit my activity and restart


Android: What is the best place to
create SQLite database and tables


What is the Default Threading mode
of SQLite in Android?


Android SQLite Example using pojo
and using CRUD operations


asked

5 months ago
viewed
4203 times


active

5 months ago
blog
Podcast #42


It

s The
Exception That Proves The
Rule

Stack Overflow works best with JavaScript enabled
Generated with www.html-to-pdf.net
Page 1 / 3
Now you can use this class as below,

Now you can use MyDB class in you activity to have all the database operations. The create records will
help you to insert the values similarly you can have your own functions for update and delete.

}

// Method is called during creation of the database

@Override

public void onCreate(SQLiteDatabase database) {

database.execSQL(DATABASE_CREATE);

}

// Method is called during an upgrade of the database,

@Override

public void onUpgrade(SQLiteDatabase database, int oldVersion,

int newVersion) {

Log.w(DatabaseHelper.class.getName(),

"Upgrading database from version " + oldVersion + " to "

+ newVersion + ", which will destroy all old data");

database.execSQL("DROP TABLE IF EXISTS MyEmployees");

onCreate(database);

}

}

public class MyDB{

private MyDatabaseHelper dbHelper;

private SQLiteDatabase database;

public final static String EMP_TABLE="MyEmployees"; // name of table

public final static String EMP_ID="_id"; // id value for employee

public final static String EMP_NAME="name"; // name of employee

/**

*

* @param context

*/

public MyDB(Context context){

dbHelper = new DatabaseHelper(context);

database = dbHelper.getWritableDatabase();

}

public long createRecords(String id, String name){

ContentValues values = new ContentValues();

values.put(EMP_ID, id);

values.put(EMP_NAME, name);

return database.insert(EMP_TABLE, null, values);

}

public selectRecords() {

String[] cols = new String[] {EMP_ID, EMP_NAME};

Cursor mCursor = database.query(true, EMP_TABLE,cols,null

, null, null, null, null, null);

if (mCursor != null) {

mCursor.moveToFirst();

}

return mCursor; // iterate to get each value.

}

share
|
improve this answer

answered

Aug 18 '12 at 4:35


JJPA

2,466
2
4
18

feedback


thanks a lot for providing me a demo skelton of the database.
It is really very useful and efficient enough to
answer to my related query.
thanks again..
Regards :)


 
Rushabh Chheda

Aug
18
'
12
at
4:37

2

The following Links my help you

1. Android Sqlite Database

2. Tutorial_1

3.Tutorial_2

Database Helper Class:

A helper class to manage database creation and version management.

You create a subclass implementing
onCreate(SQLiteDatabase)
,
onUpgrade(SQLiteDatabase,
int, int)
and optionally
onOpen(SQLiteDatabase)
, and this class takes care of opening the
Android (Dev): Copying SQLite db
from android to PC (and back
again)?


Android
-
Can't Find Table in Pre
-
made Database


Reading SQLITE database from
Android memory


Android use database from existing
sqlite database


Table not created in SQLite
Database in Android


Fetching dertails from the CACHE
(SQLITE) and display in ANDROID


Unable to open the existing
database in android


Using Sqlite on Android


Inserting highscore record into
SQLite database, nullpointer
-

Android (java)


Sqlite databse in android
appliaction working but it comes
with double entry(insert) in table


Generated with www.html-to-pdf.net
Page 2 / 3
Not the answer you're looking for?
Browse other questions tagged
android

sqlite

sqliteopenhelper
or
ask your own question
.


database if it exists, creating it if it does not, and upgrading it as necessary. Transactions are used to
make sure the database is always in a sensible state.

This class makes it easy for
ContentProvider
implementations to defer opening and upgrading the
database until first use, to avoid blocking application startup with long
-
running database upgrades.

You need more refer this link
Sqlite Helper

share
|
improve this answer

answered

Aug 18 '12 at 4:08


Aerrow

2,316
1
5
24

feedback


OK. thanks a lot for your effort..


 
Rushabh Chheda

Aug
18
'
12
at
4:11

1

Using Helper class you can access SQLite Database and can perform the various operations on it by
overriding the onCreate() and onUpgrade() methods.

share
|
improve this answer

answered

Aug 18 '12 at 4:16


wosimosi

11
1

feedback


0

The DBHelper class is what handles the opening and closing of sqlite databases as well sa creation and
updating, and a decent article on how it all works is
here
. When I started android it was very useful
(however I've been objective
-
c lately, and forgotten most of it to be any use.

share
|
improve this answer

answered

Aug 18 '12 at 4:06


CBredlow

923
2
19

feedback


OK.. thanks for the link.


 
Rushabh Chheda

Aug
18
'
12
at
4:10

site design / logo
© 
2013 stack exchange inc;
user contributions licensed under
cc
-
wiki
with
attribution required


about
|

faq
|

blog
|

chat
|

data
|

legal
|

privacy policy
|

jobs
|

advertising info
|

mobile
|

contact us
|
feedback


 
stackoverflow.com
 


 
api/apps
 


 
careers 2.0
 


 
serverfault.com
 


 
superuser.com
 


 
meta
 


 
area 51
 


 
webapps
 


 
gaming
 


 
ubuntu
 


 
webmasters
 


 
cooking
 


 
game development
 


 
math
 


 
photography
 


 
stats
 


 
tex
 


 
english
 


 
theoretical cs
 


 
programmers
 


 
unix
 


 
apple
 


 
wordpress
 


 
physics
 


 
home improvement
 


 
gis
 


 
electrical engineering
 


 
android
 


 
security
 


 
bicycles
 


 
dba
 


 
drupal
 


 
sharepoint
 


 
scifi & fantasy
 


 
user experience
 


 
skeptics
 


 
rpg
 


 
judaism
 


 
mathematica
 


 
travel
 


rev 2013.2.7.1383
Generated with www.html-to-pdf.net
Page 3 / 3