Creating database for an application is more critical part for navigating the data for retreviewing and maintianing the data source in sqlite databse when you are devloping an app then you should create a sqlite database for maintaing our database ,so the following code will helpful for creating sqlite database .
write the following code in to the mainactivity:
package com.sql.demo;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SqlitedemoActivity extends Activity implements OnClickListener {
EditText user, passlock;
Button login;
String username, pssword;
SQLiteDatabase sq;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
user = (EditText) findViewById(R.id.editText1);
passlock = (EditText) findViewById(R.id.editText2);
login = (Button) findViewById(R.id.button1);
login.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
username = user.getText().toString();
pssword = passlock.getText().toString();
// **************************
// step1:creating data base
sq = openOrCreateDatabase("MyDatabase", MODE_PRIVATE, null);
Toast.makeText(this, "Database created", 20).show();
// step2: creating table in Database
sq.execSQL("create table if not exists users(Username Text,Password Text)");
Toast.makeText(this, "Table created", 20).show();
// step3 : inserting values into table
sq.execSQL("insert into users values('" + username + "','" + pssword
+ "')");
Toast.makeText(this, "values inserted", 20).show();
// Step 4: retrieving the data
Cursor c=sq.rawQuery("select * from users", null);
do{
c.moveToFirst();
String name=c.getString(c.getColumnIndex("Username"));
String pass_user=c.getString(c.getColumnIndex("Password"));
Toast.makeText(this, "values are:::"+name+"&&&&&"+pass_user, 20).show();
}while(c.moveToNext());
// **********************
}
}
write the following code into the main.xml::::::::-----------
<?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"
android:background="#de6600">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:hint="username"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:hint="password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>
write the following code in to the mainactivity:
package com.sql.demo;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SqlitedemoActivity extends Activity implements OnClickListener {
EditText user, passlock;
Button login;
String username, pssword;
SQLiteDatabase sq;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
user = (EditText) findViewById(R.id.editText1);
passlock = (EditText) findViewById(R.id.editText2);
login = (Button) findViewById(R.id.button1);
login.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
username = user.getText().toString();
pssword = passlock.getText().toString();
// **************************
// step1:creating data base
sq = openOrCreateDatabase("MyDatabase", MODE_PRIVATE, null);
Toast.makeText(this, "Database created", 20).show();
// step2: creating table in Database
sq.execSQL("create table if not exists users(Username Text,Password Text)");
Toast.makeText(this, "Table created", 20).show();
// step3 : inserting values into table
sq.execSQL("insert into users values('" + username + "','" + pssword
+ "')");
Toast.makeText(this, "values inserted", 20).show();
// Step 4: retrieving the data
Cursor c=sq.rawQuery("select * from users", null);
do{
c.moveToFirst();
String name=c.getString(c.getColumnIndex("Username"));
String pass_user=c.getString(c.getColumnIndex("Password"));
Toast.makeText(this, "values are:::"+name+"&&&&&"+pass_user, 20).show();
}while(c.moveToNext());
// **********************
}
}
write the following code into the main.xml::::::::-----------
<?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"
android:background="#de6600">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:hint="username"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:hint="password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login" />
</LinearLayout>
No comments:
Post a Comment