ECPLISE ANDROID DATABASE CONNECTIVITY
STEPS
1. Open the Ecplise
2. Create New->Project->Android Project
4. Click Finish
4. Open the Navigator
5. Choose your project database->src->com->example->databaseconnectionactivity double click that
6. now it will show the code window like below
package com.example.databaseconnection;
import android.app.Activity;
import android.os.Bundle;
public class DatabaseconnectionActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
7.Alter the code as follows
package com.example.databaseconnection;
import android.app.Activity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;
public class DataBaseActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SQLiteDatabase db;
db = openOrCreateDatabase(
"dict"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
String select="select * from dbcontents";
Cursor cur=db.rawQuery(select, null);
cur.moveToFirst();
List<String> array = new ArrayList<String>();
while(cur.moveToNext()){
String uname = cur.getString(cur.getColumnIndex("stext"));
array.add(uname);
}
cur.close();
ListView listView = (ListView) findViewById(R.id.mylist);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, array);
// Assign adapter to ListView
listView.setAdapter(adapter);
}
}
8. Alter your main.xml file as follows
<?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" />
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
9. Finally we have to add our sqllite database to our project the database name is "dict" and the table name is "dbcontents" it contains filed name as "stext"
10. create a "dict" database and "dbcontents" table manually using "SQLite datbase browser"
10 . choose Window->Showview->File explorer or Window->Showview->Other->FileExplorer
11. In File Explorer
data->data->com.example.databaseconnection->database->
if u dont get data->data->com.example.databaseconnection just run your project then try
12. select "push a file on to the device" choose your datbase file .
13. Run and Check it.