How can i set current date, as a database table name in sqlite?












0















I'm trying to create a table that name is the current date.



Here is my table name that i try



 static String date = new SimpleDateFormat("dmmmyy", Locale.getDefault()).format(new Date());


public static String table_name=date;


but it has shown this error



unrecognized token: "26_10_2018" (code 1): , while compiling: INSERT INTO 26_10_2018(quentity_cl,price_cl,item_Cl,spinner_cl) VALUES (?,?,?,?)


My aim is to set a table name using the current date.
thanks.










share|improve this question





























    0















    I'm trying to create a table that name is the current date.



    Here is my table name that i try



     static String date = new SimpleDateFormat("dmmmyy", Locale.getDefault()).format(new Date());


    public static String table_name=date;


    but it has shown this error



    unrecognized token: "26_10_2018" (code 1): , while compiling: INSERT INTO 26_10_2018(quentity_cl,price_cl,item_Cl,spinner_cl) VALUES (?,?,?,?)


    My aim is to set a table name using the current date.
    thanks.










    share|improve this question



























      0












      0








      0








      I'm trying to create a table that name is the current date.



      Here is my table name that i try



       static String date = new SimpleDateFormat("dmmmyy", Locale.getDefault()).format(new Date());


      public static String table_name=date;


      but it has shown this error



      unrecognized token: "26_10_2018" (code 1): , while compiling: INSERT INTO 26_10_2018(quentity_cl,price_cl,item_Cl,spinner_cl) VALUES (?,?,?,?)


      My aim is to set a table name using the current date.
      thanks.










      share|improve this question
















      I'm trying to create a table that name is the current date.



      Here is my table name that i try



       static String date = new SimpleDateFormat("dmmmyy", Locale.getDefault()).format(new Date());


      public static String table_name=date;


      but it has shown this error



      unrecognized token: "26_10_2018" (code 1): , while compiling: INSERT INTO 26_10_2018(quentity_cl,price_cl,item_Cl,spinner_cl) VALUES (?,?,?,?)


      My aim is to set a table name using the current date.
      thanks.







      android sql sqlite android-studio






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 0:31







      Imran Sk

















      asked Nov 26 '18 at 0:19









      Imran SkImran Sk

      47110




      47110
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I believe that you could base what you want on the following, which get's around the fact that tables names (and names overall column's triggers ....) cannot, unless enclosed, start with numbers amongst other restrictions SQL As Understood By SQlite - SQLite Keywords:-



          public class DBHelperNewtablePerDay extends SQLiteOpenHelper {

          public static final String DBNAME = "mydb";
          public static final int DBVERSION = 1;

          public static final String COL_DAILY_ID = BaseColumns._ID;
          public static final String COl_DAILY_DATA = "_data";
          SQLiteDatabase mDB;
          private String mTodaysTable;

          DBHelperNewtablePerDay(Context context) {
          super(context, DBNAME, null, DBVERSION);
          mDB = this.getWritableDatabase();
          createTodaysTable();
          }


          @Override
          public void onCreate(SQLiteDatabase sqLiteDatabase) {

          }

          @Override
          public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

          }

          private void createTodaysTable() {
          String table_name = "[" + dateTodayinDD_MM_YYFormat() + "]";
          Cursor csr = mDB.query("sqlite_master",null,"name=?",new String{table_name},null,null,null);
          if (csr.getCount() < 1) {
          mDB.execSQL("CREATE TABLE IF NOT EXISTS " + table_name + "(" +
          COL_DAILY_ID + " INTEGER PRIMARY KEY, " +
          COl_DAILY_DATA + " TEXT " +
          ")");
          }
          mTodaysTable = table_name;
          }

          private String dateTodayinDD_MM_YYFormat() {
          return new SimpleDateFormat("dd_MM_yyyy", Locale.getDefault()).format(new Date());
          }

          public String getTodaysTableName() {
          return mTodaysTable;
          }
          }



          • Obviosuly the column names would likely need to be adjusted accordingly.

          • Note the getTodaysTableName method that returns the current day's table.

            - Note the corrected format




          Disclaimer



          This answer is in no means advocating/recommending the practice of creating tables on a daily basis.






          share|improve this answer


























          • Thanks ,it's help me

            – Imran Sk
            Nov 26 '18 at 11:46



















          1














          Table names cannot begin with a number,you could replace with:



          String tableName = "[26_10_2018]"





          share|improve this answer
























          • Yes. Columns as well cannot start with a number.

            – soynerdito
            Nov 26 '18 at 0:52











          • how i do it dynamic? I want to make a table every day.

            – Imran Sk
            Nov 26 '18 at 0:56








          • 1





            To @navylover: I have voted down your answer because it is simply wrong; table names can begin with a number, but must be quoted when they are referenced. That said, I will say to the OP, if you think you want to create a table for each date, your database design almost certainly needs to be redesigned. (So don't do that!)

            – varro
            Nov 26 '18 at 2:10













          • @ImranSk I'd advise ignoring the order to not do it. However, rather consider that there may well be a better design, such as having a column that includes the date. It's unlikely that efficiency would be an issue, using a single table, until the table is getting many thousands of rows and capability wise a table can have up to 9223372036854775807 rows (double if you tweak things to handle negative values).

            – MikeT
            Nov 26 '18 at 2:31













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473347%2fhow-can-i-set-current-date-as-a-database-table-name-in-sqlite%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I believe that you could base what you want on the following, which get's around the fact that tables names (and names overall column's triggers ....) cannot, unless enclosed, start with numbers amongst other restrictions SQL As Understood By SQlite - SQLite Keywords:-



          public class DBHelperNewtablePerDay extends SQLiteOpenHelper {

          public static final String DBNAME = "mydb";
          public static final int DBVERSION = 1;

          public static final String COL_DAILY_ID = BaseColumns._ID;
          public static final String COl_DAILY_DATA = "_data";
          SQLiteDatabase mDB;
          private String mTodaysTable;

          DBHelperNewtablePerDay(Context context) {
          super(context, DBNAME, null, DBVERSION);
          mDB = this.getWritableDatabase();
          createTodaysTable();
          }


          @Override
          public void onCreate(SQLiteDatabase sqLiteDatabase) {

          }

          @Override
          public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

          }

          private void createTodaysTable() {
          String table_name = "[" + dateTodayinDD_MM_YYFormat() + "]";
          Cursor csr = mDB.query("sqlite_master",null,"name=?",new String{table_name},null,null,null);
          if (csr.getCount() < 1) {
          mDB.execSQL("CREATE TABLE IF NOT EXISTS " + table_name + "(" +
          COL_DAILY_ID + " INTEGER PRIMARY KEY, " +
          COl_DAILY_DATA + " TEXT " +
          ")");
          }
          mTodaysTable = table_name;
          }

          private String dateTodayinDD_MM_YYFormat() {
          return new SimpleDateFormat("dd_MM_yyyy", Locale.getDefault()).format(new Date());
          }

          public String getTodaysTableName() {
          return mTodaysTable;
          }
          }



          • Obviosuly the column names would likely need to be adjusted accordingly.

          • Note the getTodaysTableName method that returns the current day's table.

            - Note the corrected format




          Disclaimer



          This answer is in no means advocating/recommending the practice of creating tables on a daily basis.






          share|improve this answer


























          • Thanks ,it's help me

            – Imran Sk
            Nov 26 '18 at 11:46
















          0














          I believe that you could base what you want on the following, which get's around the fact that tables names (and names overall column's triggers ....) cannot, unless enclosed, start with numbers amongst other restrictions SQL As Understood By SQlite - SQLite Keywords:-



          public class DBHelperNewtablePerDay extends SQLiteOpenHelper {

          public static final String DBNAME = "mydb";
          public static final int DBVERSION = 1;

          public static final String COL_DAILY_ID = BaseColumns._ID;
          public static final String COl_DAILY_DATA = "_data";
          SQLiteDatabase mDB;
          private String mTodaysTable;

          DBHelperNewtablePerDay(Context context) {
          super(context, DBNAME, null, DBVERSION);
          mDB = this.getWritableDatabase();
          createTodaysTable();
          }


          @Override
          public void onCreate(SQLiteDatabase sqLiteDatabase) {

          }

          @Override
          public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

          }

          private void createTodaysTable() {
          String table_name = "[" + dateTodayinDD_MM_YYFormat() + "]";
          Cursor csr = mDB.query("sqlite_master",null,"name=?",new String{table_name},null,null,null);
          if (csr.getCount() < 1) {
          mDB.execSQL("CREATE TABLE IF NOT EXISTS " + table_name + "(" +
          COL_DAILY_ID + " INTEGER PRIMARY KEY, " +
          COl_DAILY_DATA + " TEXT " +
          ")");
          }
          mTodaysTable = table_name;
          }

          private String dateTodayinDD_MM_YYFormat() {
          return new SimpleDateFormat("dd_MM_yyyy", Locale.getDefault()).format(new Date());
          }

          public String getTodaysTableName() {
          return mTodaysTable;
          }
          }



          • Obviosuly the column names would likely need to be adjusted accordingly.

          • Note the getTodaysTableName method that returns the current day's table.

            - Note the corrected format




          Disclaimer



          This answer is in no means advocating/recommending the practice of creating tables on a daily basis.






          share|improve this answer


























          • Thanks ,it's help me

            – Imran Sk
            Nov 26 '18 at 11:46














          0












          0








          0







          I believe that you could base what you want on the following, which get's around the fact that tables names (and names overall column's triggers ....) cannot, unless enclosed, start with numbers amongst other restrictions SQL As Understood By SQlite - SQLite Keywords:-



          public class DBHelperNewtablePerDay extends SQLiteOpenHelper {

          public static final String DBNAME = "mydb";
          public static final int DBVERSION = 1;

          public static final String COL_DAILY_ID = BaseColumns._ID;
          public static final String COl_DAILY_DATA = "_data";
          SQLiteDatabase mDB;
          private String mTodaysTable;

          DBHelperNewtablePerDay(Context context) {
          super(context, DBNAME, null, DBVERSION);
          mDB = this.getWritableDatabase();
          createTodaysTable();
          }


          @Override
          public void onCreate(SQLiteDatabase sqLiteDatabase) {

          }

          @Override
          public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

          }

          private void createTodaysTable() {
          String table_name = "[" + dateTodayinDD_MM_YYFormat() + "]";
          Cursor csr = mDB.query("sqlite_master",null,"name=?",new String{table_name},null,null,null);
          if (csr.getCount() < 1) {
          mDB.execSQL("CREATE TABLE IF NOT EXISTS " + table_name + "(" +
          COL_DAILY_ID + " INTEGER PRIMARY KEY, " +
          COl_DAILY_DATA + " TEXT " +
          ")");
          }
          mTodaysTable = table_name;
          }

          private String dateTodayinDD_MM_YYFormat() {
          return new SimpleDateFormat("dd_MM_yyyy", Locale.getDefault()).format(new Date());
          }

          public String getTodaysTableName() {
          return mTodaysTable;
          }
          }



          • Obviosuly the column names would likely need to be adjusted accordingly.

          • Note the getTodaysTableName method that returns the current day's table.

            - Note the corrected format




          Disclaimer



          This answer is in no means advocating/recommending the practice of creating tables on a daily basis.






          share|improve this answer















          I believe that you could base what you want on the following, which get's around the fact that tables names (and names overall column's triggers ....) cannot, unless enclosed, start with numbers amongst other restrictions SQL As Understood By SQlite - SQLite Keywords:-



          public class DBHelperNewtablePerDay extends SQLiteOpenHelper {

          public static final String DBNAME = "mydb";
          public static final int DBVERSION = 1;

          public static final String COL_DAILY_ID = BaseColumns._ID;
          public static final String COl_DAILY_DATA = "_data";
          SQLiteDatabase mDB;
          private String mTodaysTable;

          DBHelperNewtablePerDay(Context context) {
          super(context, DBNAME, null, DBVERSION);
          mDB = this.getWritableDatabase();
          createTodaysTable();
          }


          @Override
          public void onCreate(SQLiteDatabase sqLiteDatabase) {

          }

          @Override
          public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

          }

          private void createTodaysTable() {
          String table_name = "[" + dateTodayinDD_MM_YYFormat() + "]";
          Cursor csr = mDB.query("sqlite_master",null,"name=?",new String{table_name},null,null,null);
          if (csr.getCount() < 1) {
          mDB.execSQL("CREATE TABLE IF NOT EXISTS " + table_name + "(" +
          COL_DAILY_ID + " INTEGER PRIMARY KEY, " +
          COl_DAILY_DATA + " TEXT " +
          ")");
          }
          mTodaysTable = table_name;
          }

          private String dateTodayinDD_MM_YYFormat() {
          return new SimpleDateFormat("dd_MM_yyyy", Locale.getDefault()).format(new Date());
          }

          public String getTodaysTableName() {
          return mTodaysTable;
          }
          }



          • Obviosuly the column names would likely need to be adjusted accordingly.

          • Note the getTodaysTableName method that returns the current day's table.

            - Note the corrected format




          Disclaimer



          This answer is in no means advocating/recommending the practice of creating tables on a daily basis.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 2:24

























          answered Nov 26 '18 at 2:18









          MikeTMikeT

          17k112643




          17k112643













          • Thanks ,it's help me

            – Imran Sk
            Nov 26 '18 at 11:46



















          • Thanks ,it's help me

            – Imran Sk
            Nov 26 '18 at 11:46

















          Thanks ,it's help me

          – Imran Sk
          Nov 26 '18 at 11:46





          Thanks ,it's help me

          – Imran Sk
          Nov 26 '18 at 11:46













          1














          Table names cannot begin with a number,you could replace with:



          String tableName = "[26_10_2018]"





          share|improve this answer
























          • Yes. Columns as well cannot start with a number.

            – soynerdito
            Nov 26 '18 at 0:52











          • how i do it dynamic? I want to make a table every day.

            – Imran Sk
            Nov 26 '18 at 0:56








          • 1





            To @navylover: I have voted down your answer because it is simply wrong; table names can begin with a number, but must be quoted when they are referenced. That said, I will say to the OP, if you think you want to create a table for each date, your database design almost certainly needs to be redesigned. (So don't do that!)

            – varro
            Nov 26 '18 at 2:10













          • @ImranSk I'd advise ignoring the order to not do it. However, rather consider that there may well be a better design, such as having a column that includes the date. It's unlikely that efficiency would be an issue, using a single table, until the table is getting many thousands of rows and capability wise a table can have up to 9223372036854775807 rows (double if you tweak things to handle negative values).

            – MikeT
            Nov 26 '18 at 2:31


















          1














          Table names cannot begin with a number,you could replace with:



          String tableName = "[26_10_2018]"





          share|improve this answer
























          • Yes. Columns as well cannot start with a number.

            – soynerdito
            Nov 26 '18 at 0:52











          • how i do it dynamic? I want to make a table every day.

            – Imran Sk
            Nov 26 '18 at 0:56








          • 1





            To @navylover: I have voted down your answer because it is simply wrong; table names can begin with a number, but must be quoted when they are referenced. That said, I will say to the OP, if you think you want to create a table for each date, your database design almost certainly needs to be redesigned. (So don't do that!)

            – varro
            Nov 26 '18 at 2:10













          • @ImranSk I'd advise ignoring the order to not do it. However, rather consider that there may well be a better design, such as having a column that includes the date. It's unlikely that efficiency would be an issue, using a single table, until the table is getting many thousands of rows and capability wise a table can have up to 9223372036854775807 rows (double if you tweak things to handle negative values).

            – MikeT
            Nov 26 '18 at 2:31
















          1












          1








          1







          Table names cannot begin with a number,you could replace with:



          String tableName = "[26_10_2018]"





          share|improve this answer













          Table names cannot begin with a number,you could replace with:



          String tableName = "[26_10_2018]"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 0:47









          navylovernavylover

          3,51531119




          3,51531119













          • Yes. Columns as well cannot start with a number.

            – soynerdito
            Nov 26 '18 at 0:52











          • how i do it dynamic? I want to make a table every day.

            – Imran Sk
            Nov 26 '18 at 0:56








          • 1





            To @navylover: I have voted down your answer because it is simply wrong; table names can begin with a number, but must be quoted when they are referenced. That said, I will say to the OP, if you think you want to create a table for each date, your database design almost certainly needs to be redesigned. (So don't do that!)

            – varro
            Nov 26 '18 at 2:10













          • @ImranSk I'd advise ignoring the order to not do it. However, rather consider that there may well be a better design, such as having a column that includes the date. It's unlikely that efficiency would be an issue, using a single table, until the table is getting many thousands of rows and capability wise a table can have up to 9223372036854775807 rows (double if you tweak things to handle negative values).

            – MikeT
            Nov 26 '18 at 2:31





















          • Yes. Columns as well cannot start with a number.

            – soynerdito
            Nov 26 '18 at 0:52











          • how i do it dynamic? I want to make a table every day.

            – Imran Sk
            Nov 26 '18 at 0:56








          • 1





            To @navylover: I have voted down your answer because it is simply wrong; table names can begin with a number, but must be quoted when they are referenced. That said, I will say to the OP, if you think you want to create a table for each date, your database design almost certainly needs to be redesigned. (So don't do that!)

            – varro
            Nov 26 '18 at 2:10













          • @ImranSk I'd advise ignoring the order to not do it. However, rather consider that there may well be a better design, such as having a column that includes the date. It's unlikely that efficiency would be an issue, using a single table, until the table is getting many thousands of rows and capability wise a table can have up to 9223372036854775807 rows (double if you tweak things to handle negative values).

            – MikeT
            Nov 26 '18 at 2:31



















          Yes. Columns as well cannot start with a number.

          – soynerdito
          Nov 26 '18 at 0:52





          Yes. Columns as well cannot start with a number.

          – soynerdito
          Nov 26 '18 at 0:52













          how i do it dynamic? I want to make a table every day.

          – Imran Sk
          Nov 26 '18 at 0:56







          how i do it dynamic? I want to make a table every day.

          – Imran Sk
          Nov 26 '18 at 0:56






          1




          1





          To @navylover: I have voted down your answer because it is simply wrong; table names can begin with a number, but must be quoted when they are referenced. That said, I will say to the OP, if you think you want to create a table for each date, your database design almost certainly needs to be redesigned. (So don't do that!)

          – varro
          Nov 26 '18 at 2:10







          To @navylover: I have voted down your answer because it is simply wrong; table names can begin with a number, but must be quoted when they are referenced. That said, I will say to the OP, if you think you want to create a table for each date, your database design almost certainly needs to be redesigned. (So don't do that!)

          – varro
          Nov 26 '18 at 2:10















          @ImranSk I'd advise ignoring the order to not do it. However, rather consider that there may well be a better design, such as having a column that includes the date. It's unlikely that efficiency would be an issue, using a single table, until the table is getting many thousands of rows and capability wise a table can have up to 9223372036854775807 rows (double if you tweak things to handle negative values).

          – MikeT
          Nov 26 '18 at 2:31







          @ImranSk I'd advise ignoring the order to not do it. However, rather consider that there may well be a better design, such as having a column that includes the date. It's unlikely that efficiency would be an issue, using a single table, until the table is getting many thousands of rows and capability wise a table can have up to 9223372036854775807 rows (double if you tweak things to handle negative values).

          – MikeT
          Nov 26 '18 at 2:31




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473347%2fhow-can-i-set-current-date-as-a-database-table-name-in-sqlite%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'