RSS

Code to create and insert data into databse in Android

11 Aug

– Code shown below will create database file on the sdcard.
– We will then create table inside the database file.
– Also we can fire queries like ‘Create’ , ‘Insert’ data into db file.

private void insertSentMailDetails()
{
try
{
File sdcard = Environment.getExternalStorageDirectory();
File objFile = new File(sdcard+”/nikhil.db”);

Boolean isMyDatabase= objFile.exists();

//TODO:Check file system for file exist
if(isMyDatabase.equals(false))
{
SQLiteDatabase db = openOrCreateDatabase(sdcard+”/nikhil.db”, MODE_PRIVATE, null);
db.execSQL(“CREATE TABLE SentMail(ID INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , ‘To’ TEXT, ‘Subject’ TEXT, ‘Message’ TEXT);”);
db.execSQL(“INSERT INTO SentMail(‘To’,’Subject’,’Message’) VALUES(‘”+txtTo.getText().toString()+”‘,'”+txtSubject.getText().toString()+”‘,'”+txtMessageBody.getText().toString()+”‘);”);
db.close();
}
else
{
SQLiteDatabase db = openOrCreateDatabase(sdcard+”/nikhil.db”, MODE_PRIVATE, null);
db.execSQL(“INSERT INTO SentMail(‘To’,’Subject’,’Message’) VALUES(‘”+txtTo.getText().toString()+”‘,'”+txtSubject.getText().toString()+”‘,'”+txtMessageBody.getText().toString()+”‘);”);
db.close();
}
}

Advertisements
 
Leave a comment

Posted by on August 11, 2011 in Android Database

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: