Trouble in creating database on android using sqlopenhelper - android

I have to create tables using sqlopenhelper. When I'm running the app in the emulator, the code is not giving error, but a log message from table creation is being shown. When I try to register the app stops.
I've used the following code:
package com.example.catchthebus;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DbHelp extends SQLiteOpenHelper {
SQLiteDatabase db;
public static String DB="CatchTheBus";
public static final String Table_name="Driver";
public static final String Driver_ID="Driver_ID";
public static final String Driver_Name="Driver_Name";
public static final String Driver_Contact ="Driver_Contact";
public static final String Route_Num ="Route_Num";
public static final String Table_Create="create table" +Table_name+"("+Driver_ID +"integer PRIMARY KEY,"+Driver_Name+" varchar(25),"+Driver_Contact +"integer,"+Route_Num +"integer"+");";
public static final String LOGTAG = null;
public DbHelp(Context context) {
super(context, DB, null, 1);
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(SQLiteDatabase db) {
try
{
db.execSQL(Table_Create);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void addDriver(String name ,String contact,String Route_num) {
db.execSQL("insert into Drivers values(null, '"+name+"' ,"+contact+" , "+ Route_num+" )");
String tag = null;
Log.i(tag, "inserted value");
}
}
main activity code:
package com.example.catchthebus;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
//SqlHelp sql_helper;
//SqlHelp sql_helper1;
//SqlHelp sql2;
SQLiteOpenHelper dbhelp;
SQLiteDatabase database;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbhelp = new DbHelp(this);
database= dbhelp.getWritableDatabase();
}
#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;
}
public void Open_Passenger_Submit(View view)
{
Intent intt=new Intent(this,Submit_Passenger.class);
startActivity(intt);
//sql_helper.createPassenger();
}
public void Open_Driver_Submit(View view)
{`enter code here`
Intent intt=new Intent(this,Submit_Driver.class);
startActivity(intt);
//sql_helper.createDriver();
}
}

It looks like your Table_Create string was missing some spaces.
public static final String Table_Create="create table " +Table_name+" ("+Driver_ID +" integer PRIMARY KEY,"+Driver_Name+" TEXT,"+Driver_Contact +" integer,"+Route_Num +" integer);";

Related

Android table creation error for multiple tables i thing on create method is went wrong somewhere

ONE TABLE I CAN CREATE THERE IS NO ERROR IN EMULATOR ALSO
THIS IS MY SqliteHelper.JAVA CLASS
i am able to create one table but not able to create two table there is no error is showing im using DB browser to check
package com.example.administrator.billing;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SqliteHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="billing";
public static final String TABLE_NAME_PRODUCT_MASTER ="product_master";
public static final String TABLE_NAME_SALES ="sales_master";
//product master fields
public static final String mstr_id="mstr_id";
public static final String mstr_product_code="mstr_product_code";
public static final String mstr_product_name="mstr_product_name";
public static final String mstr_cgstper="mstr_cgstper";
public static final String mstr_sgstper="mstr_sgstper";
public static final String mstr_cessper="mstr_cessper";
public static final String mstr_defrate="mstr_defrate";
// salebill
public static final String slbl_id="slbl_id";
public static final String slbl_product_code="slbl_product_code";
public static final String slbl_qty="slbl_qty";
public static final String slbl_rate="slbl_rate";
public static final String slbl_tot="slbl_tot";
public static final String slbl_cgst="slbl_cgst";
public static final String slbl_sgst="slbl_sgst";
public static final String slbl_cess="slbl_cess";
public static final String slbl_net="slbl_net";
public SqliteHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
//SQLiteDatabase db=this.getWritableDatabase();
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS "+TABLE_NAME_PRODUCT_MASTER+" (mstr_id integer primary key AUTOINCREMENT,mstr_product_code integer,mstr_product_name text not null," +
" mstr_cgstper double(18,2), mstr_sgstper double(18,2),mstr_cessper double(18,2),mstr_defrate double(18,2))");
db.execSQL("CREATE TABLE IF NOT EXISTS "+TABLE_NAME_SALES+" (slbl_id INTEGER primary key AUTOINCREMENT,slbl_product_code integer,slbl_qty double(18,2)," +
" slbl_rate double(18,2), slbl_tot double(18,2),slbl_cgst double(18,2),slbl_sgst double(18,2)" +
",slbl_cess double(18,2),slbl_net double(18,2))");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE "+TABLE_NAME_PRODUCT_MASTER);
db.execSQL("DROP TABLE "+TABLE_NAME_SALES);
onCreate(db);
}
public boolean insertProductMaster(String Mstr_product_code,String Mstr_product_name, String Mstr_cgstper, String Mstr_sgstper, String Mstr_cessper,String Mstr_defrate){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(mstr_product_code,Mstr_product_code);
contentValues.put(mstr_product_name,Mstr_product_name);
contentValues.put(mstr_cgstper,Mstr_cgstper);
contentValues.put(mstr_sgstper,Mstr_sgstper);
contentValues.put(mstr_cessper,Mstr_cessper);
contentValues.put(mstr_defrate,Mstr_defrate);
long result=db.insert(TABLE_NAME_PRODUCT_MASTER,null,contentValues);
if(result == -1){
return false;
}else{
return true;
}
}
}
AND THIS IS MY MAIN ACTIVITY.JAVA
package com.example.administrator.billing;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
SqliteHelper myDb;
Button salebill;
Button product_master;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb=new SqliteHelper(this);
product_master=(Button) findViewById(R.id.prduct_master);
salebill=(Button) findViewById(R.id.salebill);
salebill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,SaleBill.class);
startActivity(i);
}
});
product_master.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent producti = new Intent(MainActivity.this, product_master.class);
startActivity(producti);
}
});
}
}
Android table creation error for multiple tables i thing on create method is went wrong somewhere

Please tell me what is wrong in the piece of code written below

I have written a piece of code, which populates a database. However, when i execute that code in my android device or emulator, it gives an error "sorry app1 has stopped working" Please help me out to find out what's wrong.
the MainActivity.java file
package cu_coders.app1;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
public class MainActivity extends AppCompatActivity {
public final EditText e=(EditText)findViewById((R.id.text1));
public final file1 f=new file1();
TextView t=(TextView)findViewById(R.id.textView1);
final myDB x=new myDB(this, null, null, 1);
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
Button enter=(Button)findViewById(R.id.button1);
Button delete=(Button)findViewById(R.id.button2);
enter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s = e.getText().toString();
f.set_name(s);
x.addItem(f);
t.setText(x.printTable());
}
});
}
}
A CLASS NAMED FILE1.JAVA
package cu_coders.app1;
/**
* Created by user pc on 03-10-2016.
*/
public class file1 {
private int _roll;
private String _name;
public file1() {
}
public file1(String _name) {
this._name = _name;
}
public int get_roll() {
return _roll;
}
public String get_name() {
return _name;
}
public void set_roll(int _roll) {
this._roll = _roll;
}
public void set_name(String _name) {
this._name = _name;
}
}
THE CLASS FOR DATABASE MANAGEMENT-- myDB.java
package cu_coders.app1;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by user pc on 03-10-2016.
*/
public class myDB extends SQLiteOpenHelper {
private static int DATABASE_VERSION=1;
private static String DATABASE_NAME="file.db";
private static String TABLE_FILE1="class_cse";
public static String COLUMN_KEY="_key";
public static String COLUMN_NAME="_name";
public myDB(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
String query="create table "+ TABLE_FILE1 + "(" +
COLUMN_KEY + " INTEGER PRIMARY KEY "+
COLUMN_NAME+" TEXT "+
");";
db.execSQL(query);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FILE1+";");
onCreate(db);
}
public void addItem(file1 f){
ContentValues cv=new ContentValues();
cv.put(COLUMN_NAME,f.get_name());
SQLiteDatabase db=getWritableDatabase();
db.insert(TABLE_FILE1,null,cv);
db.close();
}
/*public void deletItem(String item){
SQLiteDatabase db=getWritableDatabase();
db.execSQL("DELETE FROM "+TABLE_FILE1+" WHERE "+COLUMN_NAME+" =\" "+ item +"\";");
db.close();
} */
public String printTable(){
String dbstring="";
SQLiteDatabase db=getWritableDatabase();
String query="SELECT * FROM "+TABLE_FILE1+" WHERE 1;";
Cursor c=db.rawQuery(query,null);
c.moveToFirst();
while(!c.moveToLast()){
if(c.getString(c.getColumnIndex(COLUMN_NAME))!=null) {
dbstring += c.getString(c.getColumnIndex(COLUMN_NAME));
dbstring += '\n';
}
}
db.close();
return dbstring;
}
}
please help me out to find out what is wrong. thank you in advance.
you should change several items i guess...
change myDB Constructor to this :
public myDB(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
and in mainActivity class change all global variable like this :
private EditText e ;
private file1 f ;
private TextView t;
and in oncreate() initial them :
e=(EditText)findViewById((R.id.text1));
f=new file1();
t=(TextView)findViewById(R.id.textView1);

error while retriving data from the database SQLite in android

my database helper class is
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataHandlerDairy {
public static final String DATE = "date";
public static final String DAIRY = "dairy";
private static final String DATA_BASE_NAME = "mydairy";
private static final String TABLE_NAME = "table_dairy";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_CREATE="create table table_dairy (date text primary key," + "dairy text not null);";
DataBaseHelper1 dbhelper1;
Context ct;
SQLiteDatabase db;
public DataHandlerDairy(Context ct)
{
this.ct=ct;
dbhelper1 = new DataBaseHelper1(ct);
}
private static class DataBaseHelper1 extends SQLiteOpenHelper
{
public DataBaseHelper1(Context ct)
{
super(ct,DATA_BASE_NAME,null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try
{
db.execSQL(TABLE_CREATE);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS table_dairy ");
onCreate(db);
}
}
public DataHandlerDairy open()
{
db = dbhelper1.getWritableDatabase();
return this;
}
public void close()
{
dbhelper1.close();
}
public long insertData(String date,String dairy)
{
ContentValues content = new ContentValues();
content.put(DATE, date);
content.put(DAIRY, dairy);
return db.insertOrThrow(TABLE_NAME, null, content);
}
public Cursor returnData(String date1) throws SQLException
{
Cursor c = db.query(true, TABLE_NAME, new String[] { DATE, DAIRY}, DATE + "=" + date1, null, null, null, null, null);
if(c!=null)
{
c.moveToFirst();
}
return c;
}
}
when i try to retrive the data from the database I am getting an error.
I have used the database helper class in the following code.
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class Read_Dairy extends Activity {
String date1;
TextView tv1,tv2;
ImageButton imgb1;
Button bt1;
DataHandlerDairy handler3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read__dairy);
tv1=(TextView) findViewById(R.id.readme);
imgb1=(ImageButton) findViewById(R.id.tick);
bt1=(Button) findViewById(R.id.ready);
tv2=(TextView) findViewById(R.id.love);
Bundle bundle = getIntent().getExtras();
date1 = bundle.getString("kanna");
bt1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String getdata=" ";
tv2.setText(date1);
String abcd=tv2.getText().toString();
handler3 = new DataHandlerDairy(getBaseContext());
handler3.open();
Cursor c = handler3.returnData(abcd);
if(c.moveToFirst())
{
do
{
getdata=c.getString(1);
}while(c.moveToNext());
}
handler3.close();
if(getdata==null)
{
tv1.setText("no data exists for this date");
}
else
{
tv1.setText(getdata);
}
}
});
imgb1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent ks = new Intent(Read_Dairy.this,PickYourDate.class);
startActivity(ks);
}
});
Typeface font = Typeface.createFromAsset(getAssets(), "Strato-linked.ttf");
tv1.setTypeface(font);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_read__dairy, menu);
return true;
}
}
I am getting an error while using this and my StackTrace is
FATAL EXCEPTION: main
Process: simple.smile.my_dairy, PID: 4423
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at simple.smile.my_dairy.Read_Dairy$1.onClick(Read_Dairy.java:71)
at android.view.View.performClick(View.java:4832)
at android.view.View$PerformClick.run(View.java:19839)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5315)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736)
Please tell me where the error is and how to rectify it.
getdata=c.getString(1) is not a good way. Instead use getdata=c.getString(c.getColumnIndex(DataHandlerDairy.DAIRY))

android null pointer while using SQLITE

i am trying to implement an app in which text entered in edittext is saved to the DB and then is displayed in a text field. But the app is crashing when i try to do this and i get a null pointer exception. This is my code
package com.example.dbex;
import java.sql.SQLOutput;
import java.sql.SQLPermission;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHandler extends SQLiteOpenHelper {
public static final int DATABASE_VERSION=1;
public static final String DATABASE_NAME="contactsmanager";
public static final String TABLE_CONTACTS="contacts";
public static final String KEY_ID="id";
public static final String KEY_NAME="name";
public static final String KEY_NUMBER="number";
public DBHandler(Context context) {
super(context,DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_TABLE_CONTACTS="CREATE TABLE "+TABLE_CONTACTS+"("+KEY_ID+" INTEGER PRIMARY KEY,"+KEY_NAME+" TEXT,"+KEY_NUMBER+" TEXT"+")";
db.execSQL(CREATE_TABLE_CONTACTS);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+TABLE_CONTACTS);
onCreate(db);
}
public void addContact(Contacts contacts)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(KEY_NAME, contacts.getname());
values.put(KEY_NUMBER, contacts.getnumber());
db.insert(TABLE_CONTACTS,null,values);
db.close();
}
public Contacts getContact(int id)
{
SQLiteDatabase db=this.getReadableDatabase();
Cursor c=db.query(TABLE_CONTACTS, new String[]{KEY_ID,KEY_NAME,KEY_NUMBER},KEY_ID+" =?",new String[]{String.valueOf(id)}, null, null, null, null);
Contacts contacts=null;
if(c!=null&&c.moveToFirst())
{
contacts=new Contacts(Integer.parseInt(c.getString(0)),c.getString(1),c.getString(2));
}
return contacts;
}
public void deleteContact(Contacts contacts)
{
SQLiteDatabase db=this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID+" =?", new String[]{String.valueOf(contacts.getID())});
db.close();
}
}
and the main activity is
package com.example.dbex;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button)findViewById(R.id.button1);
EditText et1=(EditText)findViewById(R.id.editText1);
EditText et2=(EditText)findViewById(R.id.editText2);
final TextView tv1=(TextView)findViewById(R.id.textView1);
final String _name=et1.getText().toString();
final String _phone=et2.getText().toString();
final DBHandler dbh=new DBHandler(getBaseContext());
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dbh.addContact(new Contacts(_name, _phone));
String contact1=(dbh.getContact(0)).getname();
tv1.setText(contact1);
}
});
}
#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;
}
}
i am getting the null pointer exception in the line
String contact1=(dbh.getContact(0)).getname();
where is my mistake?
thanks
Since you are telling you are able to insert the value, I assume your DB creation is correct You have hardcoded the id being queried to 0. You may not be having any value here. See this post to understand that even when you delete existing rows the auto increment ID could be continuing from the last used value instead of 0. So you could be returning a null value in this part of your code:
Contacts contacts=null;
if(c!=null&&c.moveToFirst())
{
contacts=new Contacts(Integer.parseInt(c.getString(0)),c.getString(1),c.getString(2));
}
return contacts;

The source attachment does not contain the source for the file SQLiteOpenHelper.class

I had a sample app in android version 4.2. I had set Breakpoints for each and every line. At the time of debugging, suddenly the debugging stopped saying that "The source attachment does not contain the source for the file SQLiteOpenHelper.class".
I attached the jar file and still it reports the error. Installed everything currently. But dont know the cause for this error.
This is my MainActivity.java
package com.example.newwaterreadingapp;
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
protected static final android.content.Context Context = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout mainLayout=(RelativeLayout)findViewById(R.id.mainLayout);
mainLayout.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right));
final LinearLayout readingLayout=(LinearLayout)findViewById(R.id.waterReading);
readingLayout.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right));
final LinearLayout pastDatePickerLayout=(LinearLayout)findViewById(R.id.PastDatePickerLayout);
pastDatePickerLayout.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right));
readingLayout.setVisibility(View.GONE);
pastDatePickerLayout.setVisibility(View.GONE);
Button AddWaterReading=(Button)findViewById(R.id.AddWaterReading); //click on + button.
AddWaterReading.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
readingLayout.setVisibility(View.VISIBLE);
mainLayout.setVisibility(View.GONE);
TextView txtgetCurrentDate=(TextView)findViewById(R.id.currentDate);
final Calendar c=Calendar.getInstance();
txtgetCurrentDate.setText((c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DATE)+"/"+c.get(Calendar.YEAR));
}
});
TextView getPastDate=(TextView)findViewById(R.id.getDate);
getPastDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
readingLayout.setVisibility(View.GONE);
pastDatePickerLayout.setVisibility(View.VISIBLE);
}
});
Button savePastDate=(Button)findViewById(R.id.savePastDate);
savePastDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pastDatePickerLayout.setVisibility(View.GONE);
readingLayout.setVisibility(View.VISIBLE);
DatePicker getPastDatepicker=(DatePicker)findViewById(R.id.getPastDate);
int YY=getPastDatepicker.getYear();
int MM=getPastDatepicker.getMonth();
int DD=getPastDatepicker.getDayOfMonth();
TextView getPastDate=(TextView)findViewById(R.id.getDate);
getPastDate.setText((MM+1)+"/"+DD+"/"+YY);
}
});
Button saveWaterReadingToDB=(Button)findViewById(R.id.saveWaterReading);
saveWaterReadingToDB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
readingLayout.setVisibility(View.GONE);
mainLayout.setVisibility(View.VISIBLE);
TextView getPastDate=(TextView)findViewById(R.id.getDate);
TextView txtgetCurrentDate=(TextView)findViewById(R.id.currentDate);
TextView txtgetWaterReading=(TextView)findViewById(R.id.water_Reading);
CreateDB helper =new CreateDB(Context, "WaterElectricityReading.db", null, 1);
SQLiteDatabase DB;
DB=helper.getWritableDatabase();
String pastDate=getPastDate.getText().toString().trim();
String currentDate=txtgetCurrentDate.getText().toString().trim();
String waterReading=txtgetWaterReading.getText().toString().trim();
ContentValues values=new ContentValues();
values.put(CreateDB.COLUMN_NAME_READING_MODE,"Water");
values.put(CreateDB.COLUMN_NAME_PASTDATETIME, pastDate);
values.put(CreateDB.COLUMN_NAME_CURRENTDATETIME, currentDate);
values.put(CreateDB.COLUMN_NAME_READINGVALUE, waterReading);
DB.insert(CreateDB.TABLE_NAME, null, values);
Toast.makeText(getApplicationContext(), "Added", Toast.LENGTH_LONG).show();
DB.close();
helper.close();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and CreateDB.java
package com.example.newwaterreadingapp;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class CreateDB extends SQLiteOpenHelper{
private static final String DATABASE_NAME="WaterElectricityReading.db";
public static final String TABLE_NAME="Reading";
public static final String COLUMN_NAME_READING_ID="_Id";
public static final String COLUMN_NAME_READING_MODE="ReadingMode";
public static final String COLUMN_NAME_PASTDATETIME="PastDateTime";
public static final String COLUMN_NAME_CURRENTDATETIME="CurrentDateTime";
public static final String COLUMN_NAME_READINGVALUE="ReadingValue";
public static final int DATABASE_VERSION = 1;
public static String DB_PATH = "";
public CreateDB(Context context,String name, CursorFactory factory,
int version) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase DB){
final String CREATE_TABLE="create table if not exists "+ TABLE_NAME + "("+ COLUMN_NAME_READING_ID +" integer primary key autoincrement,"
+COLUMN_NAME_READING_MODE+" text not null,"+COLUMN_NAME_PASTDATETIME+" date not null,"+COLUMN_NAME_CURRENTDATETIME
+" date not null,"+COLUMN_NAME_READINGVALUE+" integer not null"+");";
DB.execSQL(CREATE_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
Could anyone please suggest where I went wrong.
Thanks.
A .jar does not expose its source code by itself. You'll have to add the source code by yourself to be able to browse it.
You can be pretty sure though that the mistake will be on your own side, so you could question the use of debugging Android code :)
You can add it manually (don't remember how, Google) or use an Eclipse plugin which did a good job for me in the past; https://code.google.com/p/adt-addons/
Might be a little out-dated though.

Categories

Resources