• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem in creating a database..

kiranuday43

Newbie
Mar 3, 2010
20
0
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) {


}


}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones