kiranuday43
Newbie
anyOne tel me,,,how to create database,,,
mycode is not working
package com.android.DataBaseWork;
import java.io.FileNotFoundException;
import java.sql.ResultSet;
import java.util.ArrayList;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.android.DataBaseWork.R.string;
public class DatabaseWork extends ListActivity {
/** Called when the activity is first created. */
private final String DATABASE_NAME="uday_db";
private final String DATABASE_TABLE="Employee";
private final String DATABASE_VERSION="1";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<string> list=new ArrayList<string>();
SQLiteDatabase myDB=null;
try
{
this.createDatabase(DATABASE_NAME, DATABASE_VERSION, MODE_PRIVATE, null);
myDB=this.openDatabase(DATABASE_NAME,null);
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ DATABASE_TABLE
+ " (LastName VARCHAR, FirstName VARCHAR,"
+ " Country VARCHAR, Age INT(3));");
myDB.execSQL("INSERT INTO "
+ DATABASE_TABLE
+ " (LastName, FirstName, Country, Age)"
+ " VALUES ('Gramlich', 'Nicolas', 'Germany', 20);");
myDB.execSQL("INSERT INTO "
+ DATABASE_TABLE
+ " (LastName, FirstName, Country, Age)"
+ " VALUES ('Doe', 'John', 'US', 34);");
String str="select * from DATABASE_TABLE";
Cursor c ;
ResultSet rs;
rs=c.query(str);
rs.getString(1);
// int firstNameColumn = c.getColumnIndex("FirstName");
// int ageColumn = c.getColumnIndex("Age");
/*Check if our result was valid. */
if (c != null) {
/* Check if at least one Result was returned. */
if (c.first()) {
int i = 0;
/* Loop through all Results */
do {
i++;
/* Retrieve the values of the Entry
* the Cursor is pointing to. */
String firstName = c.getString(firstNameColumn);
int age = c.getInt(ageColumn);
/* We can also receive the Name
* of a Column by its Index.
* Makes no sense, as we already
* know the Name, but just to shwo we can Wink */
String ageColumName = c.getColumnName(ageColumn);
/* Add current Entry to results. */
results.add("" + i + ": " + firstName
+ " (" + ageColumName + ": " + age + ")");
} while (c.next());
}
}
}
catch (FileNotFoundException e) {
} finally {
if (myDB != null)
myDB.close();
}
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
}
private SQLiteDatabase openDatabase(String dATABASENAME, Object object) {
return null;
}
private void createDatabase(String dATABASENAME, String dATABASEVERSION,
int modePrivate, Object object) {
}
}
mycode is not working
package com.android.DataBaseWork;
import java.io.FileNotFoundException;
import java.sql.ResultSet;
import java.util.ArrayList;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.android.DataBaseWork.R.string;
public class DatabaseWork extends ListActivity {
/** Called when the activity is first created. */
private final String DATABASE_NAME="uday_db";
private final String DATABASE_TABLE="Employee";
private final String DATABASE_VERSION="1";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<string> list=new ArrayList<string>();
SQLiteDatabase myDB=null;
try
{
this.createDatabase(DATABASE_NAME, DATABASE_VERSION, MODE_PRIVATE, null);
myDB=this.openDatabase(DATABASE_NAME,null);
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ DATABASE_TABLE
+ " (LastName VARCHAR, FirstName VARCHAR,"
+ " Country VARCHAR, Age INT(3));");
myDB.execSQL("INSERT INTO "
+ DATABASE_TABLE
+ " (LastName, FirstName, Country, Age)"
+ " VALUES ('Gramlich', 'Nicolas', 'Germany', 20);");
myDB.execSQL("INSERT INTO "
+ DATABASE_TABLE
+ " (LastName, FirstName, Country, Age)"
+ " VALUES ('Doe', 'John', 'US', 34);");
String str="select * from DATABASE_TABLE";
Cursor c ;
ResultSet rs;
rs=c.query(str);
rs.getString(1);
// int firstNameColumn = c.getColumnIndex("FirstName");
// int ageColumn = c.getColumnIndex("Age");
/*Check if our result was valid. */
if (c != null) {
/* Check if at least one Result was returned. */
if (c.first()) {
int i = 0;
/* Loop through all Results */
do {
i++;
/* Retrieve the values of the Entry
* the Cursor is pointing to. */
String firstName = c.getString(firstNameColumn);
int age = c.getInt(ageColumn);
/* We can also receive the Name
* of a Column by its Index.
* Makes no sense, as we already
* know the Name, but just to shwo we can Wink */
String ageColumName = c.getColumnName(ageColumn);
/* Add current Entry to results. */
results.add("" + i + ": " + firstName
+ " (" + ageColumName + ": " + age + ")");
} while (c.next());
}
}
}
catch (FileNotFoundException e) {
} finally {
if (myDB != null)
myDB.close();
}
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
}
private SQLiteDatabase openDatabase(String dATABASENAME, Object object) {
return null;
}
private void createDatabase(String dATABASENAME, String dATABASEVERSION,
int modePrivate, Object object) {
}
}