Android : Creating Database in Android and Data Insertion

on Sunday, September 14, 2014


I was writing a simple programme for Creating Database and Data Insertion,but i am not able to Create the Database and I am getting the "Error" from Catch Block.Even I am not able to search for my database in file explorer,but it isnot showing there.As,I am new to Android,may be some mistake has happened.Please look into code and let me know where I missed.Thanks in advance.


Code is as:


Layout File:



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.dataandroid.MainActivity"
tools:ignore="MergeRootFrame" >

<TextView
android:id="@+id/txtname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/labelOne"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >

<requestFocus />
</EditText>

<TextView
android:id="@+id/txtAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/labelTwo"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />

<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />

<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />

</LinearLayout>


Main Activity:



package com.example.dataandroid;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
EditText name,age;
Button submit,save;
myDBHelper dbHelper;

@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.name);
submit=(Button)findViewById(R.id.btnSubmit);
save=(Button)findViewById(R.id.btnSave);
age=(EditText)findViewById(R.id.age);
dbHelper=new myDBHelper(this);

save.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String n= name.getText().toString();
String a= age.getText().toString();

try
{
dbHelper.insertContact(n,a);
}
catch(Exception e)
{

Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
}
});

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}

}

DatabaseHelperClass:

package com.example.dataandroid;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class myDBHelper extends SQLiteOpenHelper{


private static final String DB_Name="mydb";
private static final int version=1;

private static final String table_name="Details";
private static final String Col_Name="Name";
private static final String COL_Age="Age";

//private static final String String_Create="Create Table" + table_name + "(_id
Integer Primary Key AUTO INCREMENT," + Col_Name + "TEXT , " + COL_Age + "TEXT);";
public myDBHelper(Context context) {
super(context, DB_Name, null, version);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String String_Create="Create Table" + table_name + "(_id Integer Primary Key AUTO
INCREMENT," + Col_Name + "TEXT , " + COL_Age + "TEXT);";
db.execSQL(String_Create);
/* ContentValues cv=new ContentValues();
cv.put(Col_Name," Name");
cv.put(COL_Age, "Age");
db.insert(table_name, null,cv);*/
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("Drop Table if exists" + table_name);
onCreate(db);

}
private SQLiteDatabase db;
public long insertContact(String name,String age)
{
ContentValues cv=new ContentValues();
cv.put(Col_Name,name);
cv.put(COL_Age,age);
return db.insert(table_name, null,cv);
}

}

Please help me as i have searched all the tutorials but not able to find any solution.

0 comments:

Post a Comment