3

I'm new to Android Programming and I am kind of lost with the SQLite table creation.So in my app,user enters three values(Intime,OutTime and Date) which should be saved to the table "updatedata" under Record.db Here is my code for that:

public class Inserted_DB{

public static final String dbname="Record" ;
public static final String table="updatedata";
public static final String in="InTime";
public static final String out="OutTime";
public static final String date="Date";
SQLiteDatabase db;
Context ctx;
helper help;

public Inserted_DB(Context ctx)
{
    this.ctx=ctx;
    help=new helper(ctx);
}


private static class helper extends SQLiteOpenHelper
{
    public helper(Context ctx)

    {
        super(ctx,dbname,null,2);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {

       try
       {
           String sql = "create table "+ table  +"(InTime text,OutTime text,Date text)";
           db.execSQL(sql);

       }
       catch(Exception e1)
        {
            e1.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("DROP TABLE IF EXISTS " + table);
        onCreate(db);
    }
}
 public void update(String In1,String out1,String date1,Context ct)
 {
   db=help.getWritableDatabase();
   ContentValues con=new ContentValues();
   con.put(in,In1);
   con.put(out,out1);
   con.put(date,date1);
   db.insert(table,null,con);
   db.close();
  }}

I am just calling the update function from another class(Update Tracker) and the snippet is below:

            String in_db=txtTime.getText().toString();
            String date_db=txtDate.getText().toString();
            String out_db=txtTime2.getText().toString();
            Inserted_DB db1=new Inserted_DB(getBaseContext());
            db1.update(in_db,out_db,date_db,UpdateTracker.this);

DB getting created but the table is not.I used DDMS File explorer to navigate to the respective path data/data/mypackage.com.myapp/databases and could see the DB created.Using sqlite3 when I query ".databases",its showing the DB name but when I query ".tables" its empty.

Updated Snap for DB getting created

What am I missing?Could you guys help me out to find a solution!

Thanks in advance!

Sriram S
  • 169
  • 14
  • Try deleting the database and re run. Check this if this helps: http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run – proy31 Jan 02 '16 at 09:06
  • 1
    Note your database file name is `Record`. The `Record.db` seen in your DDMS is some older, empty file. – laalto Jan 02 '16 at 09:07
  • I un-installed the app and installed again.Also I cleared the app data.But unfortunately its not getting created – Sriram S Jan 02 '16 at 09:12
  • 1
    Anyway, your helper declares the name as `Record` and that's the file you should be looking at with sqlite3, not `Record.db` that is created by some other code not shown here. – laalto Jan 02 '16 at 09:22
  • Oh I just thought .db extension will be added automatically to the DB name what we give in the code.Thanks for your input laalto.Now its clear. – Sriram S Jan 02 '16 at 09:46

1 Answers1

1

I think the issues is because of space while creating table

Just Replace this

create table "+ table  +" (InTime text,OutTime text,Date text)

instead of

create table "+ table  +"(InTime text,OutTime text,Date text)

EDIT

also change database name

public static final String dbname="Record.db";

instead of

public static final String dbname="Record" ;
Dixit Patel
  • 3,190
  • 1
  • 24
  • 36
  • use this example for following standard database implementations : https://github.com/ddpatel2606/AndroidSqliteInsertTime – Dixit Patel Jan 02 '16 at 08:54
  • Thank for the instant suggestion. As you told,I just used this modified query in the respective line. String sql = "create table "+ table +" (InTime text,OutTime text,Date text)"; But still its not getting created. – Sriram S Jan 02 '16 at 09:02
  • 3
    Whitespace does not matter here. – laalto Jan 02 '16 at 09:08