A guide to Android sqlite: contents( course outline)
Part 1: A guide to Android SQLite( part 1: what it is)
Part 2: A guide to Android SQLite ( part 2: Creating the database)
Part 4: A guide to Android SQLite ( part 4: Displaying content from the database)
Part 5: A guide to Android SQLite ( part 5: Reading an existing database)
Part 6: A guide to Android SQLite ( part 6: how cursors work)
Part 7: A guide to Android SQLite ( part 7: Living with SQLite on your own)
Part 8: A guide to Android SQLite ( part 8: SQL - the basics)
Methods used for writing into a database
1. Using rawQuery
2. Using execSQL
3. Using Android methods
The SQL "insert into" query
1. Inserting values in SQLite using a simple button
package com.rea;
import android.database.sqlite.SQLiteDatabase;
import android.database.SQLException;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.content.Context;
import android.database.sqlite.SQLiteException;
public class MainActivity extends Activity {
// Cursor c;
Context cc= this;
public static SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
((Button) findViewById(R.id.button01)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b dbhelp= new b(cc);
try {
SQLiteDatabase db= dbhelp.getWritableDatabase();
db.rawQuery("insert into emmy values(50,'takan')",null);
//db.close();
Toast.makeText(cc, "Successfully created", Toast.LENGTH_SHORT).show();
} catch (SQLiteException ioe) {
//throw new Error("Unable to create database");
Toast.makeText(cc, "db not created", Toast.LENGTH_SHORT).show();
}
try{
}
catch(SQLException e){
Toast.makeText(cc, "query jode", Toast.LENGTH_SHORT).show();
}
}
});
}
}
2. Using an edit text to get user input
package com.rea;
import android.database.sqlite.SQLiteDatabase;
import android.database.SQLException;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.content.Context;
import android.database.sqlite.SQLiteException;
public class MainActivity extends Activity {
// Cursor c;
Context cc= this;
public static SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
((Button) findViewById(R.id.button01)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText edt= (EditText) findViewById(R.id.textg);
String valu=edt.getText().toString();
b dbhelp= new b(cc);
try {
db= dbhelp.getWritableDatabase();
//db.close();
Toast.makeText(cc, "Successfully created", Toast.LENGTH_SHORT).show();
} catch (SQLiteException ioe) {
//throw new Error("Unable to create database");
Toast.makeText(cc, "db not created", Toast.LENGTH_SHORT).show();
}
try{
db.rawQuery("insert into emmy values(50,\""+valu+"\")",null);
}
catch(SQLException e){
Toast.makeText(cc, "query jode", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Layout for the edittext and button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!" />
<EditText
android:id="@+id/textg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="enter ur text" />
<Button
android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click" />
</LinearLayout>
Comments
Post a Comment
Put your comment here