SOLlite CONNECTION ANDROID EXAMPLE
First Create main.xml file....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".ACTIVITY_NAME" >
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="60dp"
android:text="Connection"
android:textSize="25dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="80dp"
android:text="E_N :"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="58dp"
android:text="E_NO :"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView1"
android:ems="10"
android:hint="Enter name.." />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
android:hint="Enter no.." >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/editText2"
android:layout_marginTop="36dp"
android:text="Insert" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_marginLeft="40dp"
android:layout_toRightOf="@+id/button1"
android:text="Delete" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
android:text="update" />
</RelativeLayout>
</LinearLayout>
After Create MainActivity.java file....
public class ConnActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
EditText eno, ename;
Button b, b1, b2, b3;
ContentValues cv = null;
ListView l;
AlertDialog.Builder a;
SQLiteDatabase db;
String str = " CREATE TABLE IF NOT EXISTS STUDENT(_id INTEGER PRIMARY KEY AUTOINCREMENT,NO INTEGER,NAME TEXT)";// create table.....
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
eno = (EditText) findViewById(R.id.editText2);
ename = (EditText) findViewById(R.id.editText1);
l = (ListView) findViewById(R.id.listView1);
b = (Button) findViewById(R.id.button1);
b1 = (Button) findViewById(R.id.button2);
b2 = (Button) findViewById(R.id.button3);
b3 = (Button) findViewById(R.id.button4);
a = new AlertDialog.Builder(this);
db = openOrCreateDatabase("st", SQLiteDatabase.CREATE_IF_NECESSARY,
null);
db.execSQL(str);// create database and execute query....
b.setOnClickListener(this);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
// l.setOnItemClickListener(this);
}
public void onClick(View arg0) {
if (arg0 == b) {
cv = new ContentValues();
// Log.e("No is:-",eno.getText().toString());
// Log.e("Name is:-",ename.getText().toString());
cv.put("NO", Integer.parseInt(eno.getText().toString()));
cv.put("NAME", ename.getText().toString());
// Log.e("Data",cv.toString());
db.insert("STUDENT", null, cv);// Insert value....
ename.setText("");
eno.setText("");
Intent i = new Intent(this, ne.class);
startActivity(i);
}
if (b1 == arg0) {
cv = new ContentValues();
// cv.put("NO",Integer.parseInt(eno.getText().toString()));
db.delete("STUDENT",
"NO=" + Integer.parseInt(eno.getText().toString()), null);/Delete Value
ename.setText("");
eno.setText("");
Toast.makeText(this, "Record is delete", Toast.LENGTH_LONG).show();
}
if (b2 == arg0) {
cv = new ContentValues();
cv.put("NAME", ename.getText().toString());
db.update("STUDENT", cv,
"NO=" + Integer.parseInt(eno.getText().toString()), null);/Update Value
ename.setText("");
eno.setText("");
Toast.makeText(getApplicationContext(),
"Record Sucessufully Update", Toast.LENGTH_LONG).show();
}
if (b3 == arg0) {
}
}
}
First Create main.xml file....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".ACTIVITY_NAME" >
<TextView
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="60dp"
android:text="Connection"
android:textSize="25dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="80dp"
android:text="E_N :"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="58dp"
android:text="E_NO :"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView1"
android:ems="10"
android:hint="Enter name.." />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
android:hint="Enter no.." >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/editText2"
android:layout_marginTop="36dp"
android:text="Insert" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_marginLeft="40dp"
android:layout_toRightOf="@+id/button1"
android:text="Delete" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
android:text="update" />
</RelativeLayout>
</LinearLayout>
After Create MainActivity.java file....
public class ConnActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
EditText eno, ename;
Button b, b1, b2, b3;
ContentValues cv = null;
ListView l;
AlertDialog.Builder a;
SQLiteDatabase db;
String str = " CREATE TABLE IF NOT EXISTS STUDENT(_id INTEGER PRIMARY KEY AUTOINCREMENT,NO INTEGER,NAME TEXT)";// create table.....
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
eno = (EditText) findViewById(R.id.editText2);
ename = (EditText) findViewById(R.id.editText1);
l = (ListView) findViewById(R.id.listView1);
b = (Button) findViewById(R.id.button1);
b1 = (Button) findViewById(R.id.button2);
b2 = (Button) findViewById(R.id.button3);
b3 = (Button) findViewById(R.id.button4);
a = new AlertDialog.Builder(this);
db = openOrCreateDatabase("st", SQLiteDatabase.CREATE_IF_NECESSARY,
null);
db.execSQL(str);// create database and execute query....
b.setOnClickListener(this);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
// l.setOnItemClickListener(this);
}
public void onClick(View arg0) {
if (arg0 == b) {
cv = new ContentValues();
// Log.e("No is:-",eno.getText().toString());
// Log.e("Name is:-",ename.getText().toString());
cv.put("NO", Integer.parseInt(eno.getText().toString()));
cv.put("NAME", ename.getText().toString());
// Log.e("Data",cv.toString());
db.insert("STUDENT", null, cv);// Insert value....
ename.setText("");
eno.setText("");
Intent i = new Intent(this, ne.class);
startActivity(i);
}
if (b1 == arg0) {
cv = new ContentValues();
// cv.put("NO",Integer.parseInt(eno.getText().toString()));
db.delete("STUDENT",
"NO=" + Integer.parseInt(eno.getText().toString()), null);/Delete Value
ename.setText("");
eno.setText("");
Toast.makeText(this, "Record is delete", Toast.LENGTH_LONG).show();
}
if (b2 == arg0) {
cv = new ContentValues();
cv.put("NAME", ename.getText().toString());
db.update("STUDENT", cv,
"NO=" + Integer.parseInt(eno.getText().toString()), null);/Update Value
ename.setText("");
eno.setText("");
Toast.makeText(getApplicationContext(),
"Record Sucessufully Update", Toast.LENGTH_LONG).show();
}
if (b3 == arg0) {
}
}
}
GOOD TUTORIAL
ReplyDelete