mainActivity class
=====================
public class main extends Activity
{
TextView textvalue ;
EventDataSQLHelper eventsData;
private static String DB_PATH = "/data/data/com.myandroid.tablecreate/databases/";
private static String DB_NAME = "myDatabase.db";
/** Called when the activity is first created. */
String[] roll={"1","2","3"};
String[] name={"Ram Roy","Shyam Sen ","Samu Rai"};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textvalue = (TextView) findViewById(R.id.textView1);
eventsData = new EventDataSQLHelper(this, null, null, 1);
if(checkDataBase())
{
System.out.println(" \n up on loop......");
}
for(int i=0;i<roll.length;i++)
{
addEvent(name[i],roll[i]);
}
System.out.println(" \n dwon on loop......");
Cursor cursor = getEvents();
showEvents(cursor);
}
private void addEvent(String name,String roll) {
SQLiteDatabase db = eventsData.getWritableDatabase();
ContentValues values = new ContentValues();
// values.put(EventDataSQLHelper.Name, System.currentTimeMillis());
values.put(EventDataSQLHelper.Name, name);
values.put(EventDataSQLHelper.Roll, roll);
db.insert(EventDataSQLHelper.TABLE, null, values);
}
private Cursor getEvents() {
SQLiteDatabase db = eventsData.getReadableDatabase();
Cursor cursor = db.query(EventDataSQLHelper.TABLE, null, null, null, null,
null, null);
startManagingCursor(cursor);
return cursor;
}
private void showEvents(Cursor cursor)
{
String Roll1;
StringBuilder ret = new StringBuilder("Saved Events:\n\n");
while (cursor.moveToNext()) {
System.out.println(" Name = "+cursor.getString(1));
System.out.println(" Roll = "+cursor.getLong(2));
long id = cursor.getLong(0);
String Name1=cursor.getString(1);
String Roll = cursor.getString(2);
ret.append("id : " + id+ " Name : "+Name1+" Roll :" + Roll + "\n");
}
textvalue.setText(ret);
/* cursor.moveToLast();
String Name1=cursor.getString(1);
System.out.println("Name : "+Name1); */
}
@Override
public void onDestroy() {
eventsData.close();
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try
{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}catch (Exception e){
System.out.println("database does't exist yet.");
}
if (checkDB != null){
checkDB.close();
System.out.println("My db is:- " + checkDB.isOpen());
return true;
}
else
return false;
}
}
///////////////////////////////////////////////////////
in this class use getdata() for geting data from the table
and showdata is use for show data from the table
//////////////////////////////////create EventDataSQLHelper .class for create table ////////////////////////
====================================================================
public class EventDataSQLHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "testtable.db";
private static final int DATABASE_VERSION = 1;
// Table name
public static final String TABLE = "mytable";
// Columns
public static final String Name = "Name";
public static final String Roll = "Roll";
public EventDataSQLHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table " + TABLE + "( " + BaseColumns._ID
+ " integer primary key autoincrement, " + Name + " text, "
+ Roll + " text not null);";
Log.d("EventsData", "onCreate: " + sql);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion >= newVersion)
return;
String sql = null;
if (oldVersion == 1)
sql = "alter table " + TABLE + " add note text;";
if (oldVersion == 2)
sql = "";
Log.d("EventsData", "onUpgrade : " + sql);
if (sql != null)
db.execSQL(sql);
}
}
///////////desing for main.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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Table Value "
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
=============================================================
//////////////////////////////////////////////////manifest.xml of this application ///////////////////////////////////
=============================================================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myandroid.tablecreate"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
=====================
public class main extends Activity
{
TextView textvalue ;
EventDataSQLHelper eventsData;
private static String DB_PATH = "/data/data/com.myandroid.tablecreate/databases/";
private static String DB_NAME = "myDatabase.db";
/** Called when the activity is first created. */
String[] roll={"1","2","3"};
String[] name={"Ram Roy","Shyam Sen ","Samu Rai"};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textvalue = (TextView) findViewById(R.id.textView1);
eventsData = new EventDataSQLHelper(this, null, null, 1);
if(checkDataBase())
{
System.out.println(" \n up on loop......");
}
for(int i=0;i<roll.length;i++)
{
addEvent(name[i],roll[i]);
}
System.out.println(" \n dwon on loop......");
Cursor cursor = getEvents();
showEvents(cursor);
}
private void addEvent(String name,String roll) {
SQLiteDatabase db = eventsData.getWritableDatabase();
ContentValues values = new ContentValues();
// values.put(EventDataSQLHelper.Name, System.currentTimeMillis());
values.put(EventDataSQLHelper.Name, name);
values.put(EventDataSQLHelper.Roll, roll);
db.insert(EventDataSQLHelper.TABLE, null, values);
}
private Cursor getEvents() {
SQLiteDatabase db = eventsData.getReadableDatabase();
Cursor cursor = db.query(EventDataSQLHelper.TABLE, null, null, null, null,
null, null);
startManagingCursor(cursor);
return cursor;
}
private void showEvents(Cursor cursor)
{
String Roll1;
StringBuilder ret = new StringBuilder("Saved Events:\n\n");
while (cursor.moveToNext()) {
System.out.println(" Name = "+cursor.getString(1));
System.out.println(" Roll = "+cursor.getLong(2));
long id = cursor.getLong(0);
String Name1=cursor.getString(1);
String Roll = cursor.getString(2);
ret.append("id : " + id+ " Name : "+Name1+" Roll :" + Roll + "\n");
}
textvalue.setText(ret);
/* cursor.moveToLast();
String Name1=cursor.getString(1);
System.out.println("Name : "+Name1); */
}
@Override
public void onDestroy() {
eventsData.close();
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try
{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}catch (Exception e){
System.out.println("database does't exist yet.");
}
if (checkDB != null){
checkDB.close();
System.out.println("My db is:- " + checkDB.isOpen());
return true;
}
else
return false;
}
}
///////////////////////////////////////////////////////
in this class use getdata() for geting data from the table
and showdata is use for show data from the table
//////////////////////////////////create EventDataSQLHelper .class for create table ////////////////////////
====================================================================
public class EventDataSQLHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "testtable.db";
private static final int DATABASE_VERSION = 1;
// Table name
public static final String TABLE = "mytable";
// Columns
public static final String Name = "Name";
public static final String Roll = "Roll";
public EventDataSQLHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table " + TABLE + "( " + BaseColumns._ID
+ " integer primary key autoincrement, " + Name + " text, "
+ Roll + " text not null);";
Log.d("EventsData", "onCreate: " + sql);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion >= newVersion)
return;
String sql = null;
if (oldVersion == 1)
sql = "alter table " + TABLE + " add note text;";
if (oldVersion == 2)
sql = "";
Log.d("EventsData", "onUpgrade : " + sql);
if (sql != null)
db.execSQL(sql);
}
}
///////////desing for main.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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Table Value "
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
=============================================================
//////////////////////////////////////////////////manifest.xml of this application ///////////////////////////////////
=============================================================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myandroid.tablecreate"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment