lundi 29 juin 2015

Creating SQLite table with date as table name in Android

Can we create SQLite database table with today's date as Table name? I have tried it but it throws error. Is there a way to create table with current date as table name? My Code is

My Activity is

public class MainActivity extends ActionBarActivity {

EditText et;
Button btn;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final String date=DateFormat.getDateInstance().format(new Date());

    databaseHelper=new DatabaseHelper(this);
    final SQLiteDatabase db=databaseHelper.getReadableDatabase();

    et=(EditText)findViewById(R.id.editText);
    btn=(Button)findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String str=et.getText().toString();
            if (str.equals("")){
                Toast.makeText(MainActivity.this,"NULL",Toast.LENGTH_LONG).show();
            }else {
                ContentValues cv=new ContentValues();
                try {
                    cv.put("name", str);
                    long insert=db.insert(date, null, cv);
                    if (insert!=-1){
                        Toast.makeText(MainActivity.this,"Done",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(MainActivity.this,"Failed",Toast.LENGTH_SHORT).show();
                    }
                }catch (SQLiteException e){
                    Toast.makeText(MainActivity.this,"Error",Toast.LENGTH_SHORT).show();
                }
            }
        }
    });
}

My SQLiteOpenHelper is

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String db_name="unitdb";
public static final int version=1;
Context context;

public DatabaseHelper(Context context) {
    super(context, db_name, null, version);
    this.context=context;
}

@Override
public void onCreate(SQLiteDatabase db) {

    String date= DateFormat.getDateInstance().format(new Date());

    db.execSQL("create table '"+date+"'(name TEXT)");
    Toast.makeText(context,"DB Created",Toast.LENGTH_SHORT).show();
    Log.i("dbcreate", "Database Created");
    Log.i("Table Created", "Bike Tracker Table Created");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion>=newVersion)

        return;

    if (oldVersion==1){

        Log.d("New Version","Data's can be Upgraded");

    }

    Log.d("Sample Data", "onUpgrade:" + newVersion);

}

}

My Log cat is

06-29 17:19:03.802    1589-1589/? E/SQLiteLog﹕ (1) near "29": syntax error

06-29 17:19:03.919 355-407/? W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client 06-29 17:19:04.084 1589-1589/? E/SQLiteDatabase﹕ Error inserting name=dai android.database.sqlite.SQLiteException: near "29": syntax error (code 1): , while compiling: INSERT INTO Jun 29, 2015(name) VALUES (?) at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31) at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469) at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341) at com.realtech.test.MainActivity$1.onClick(MainActivity.java:48) at android.view.View.performClick(View.java:4780) at android.view.View$PerformClick.run(View.java:19866) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5257) 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:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Aucun commentaire:

Enregistrer un commentaire