首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >当系统日期更改时,在SQLite中创建新表?

当系统日期更改时,在SQLite中创建新表?
EN

Stack Overflow用户
提问于 2018-08-07 01:42:31
回答 2查看 0关注 0票数 0

我的问题是,当用户的系统日期更改时,或者更准确地说,在新的一年开始时,我如何创建一个自动创建表的机制?

代码语言:txt
复制
try {
  cur = db.query(TABLE_NAME, PROJECTION, SELECTION, ARGS, null, null, null);
 } catch (SQLiteException e) {
   if (e.getMessage().contains("no such table")){
     // create new table and execute query
  }
}

EN

回答 2

Stack Overflow用户

发布于 2018-08-07 09:49:39

以下内容将更具弹性(考虑到SQLite如何满足向后兼容性):

代码语言:txt
复制
cur = db.query(sqlite_master,new String{"tbl_name"},"tbl_name=?",new String[]{TABLE_NAME},null,null,null);
if (cur.getCount < 1) { // ==0 if you prefer
    //Create new table
}
票数 0
EN

Stack Overflow用户

发布于 2018-08-07 10:57:03

更多详情可在https://developer.android.com/reference/android/content/Intent.html#ACTION_日期_变化

代码:

代码语言:txt
复制
public void createDynamicDatabase(Context context,String tableName,ArrayList<String> title) {

                Log.i("INSIDE createLoginDatabase() Method","*************creatLoginDatabase*********");
                try {

                    int i;
                    String queryString;
                    myDataBase = context.openOrCreateDatabase("Db",Context.MODE_WORLD_WRITEABLE, null);         //Opens database in writable mode.
                    //System.out.println("Table Name : "+tableName.get(0));

                    queryString = title.get(0)+" VARCHAR(30),";
                    Log.d("**createDynamicDatabase", "in oncreate");

                    for(i = 1; i < title.size() - 1; i++)
                    {               
                        queryString += title.get(i);
                        queryString +=" VARCHAR(30)";
                        queryString +=",";
                    }

                    queryString+= title.get(i) +" VARCHAR(30)";

                    queryString = "CREATE TABLE IF NOT EXISTS " + tableName + "("+queryString+");";

                    System.out.println("Create Table Stmt : "+ queryString);

                    myDataBase.execSQL(queryString);

                } catch (SQLException ex) {
                    Log.i("CreateDB Exception ",ex.getMessage());
                }
            }

            public void insert(Context context,ArrayList<String> array_vals,ArrayList<String> title,String TABLE_NAME) {
                Log.d("Inside Insert","Insertion starts for table name: "+TABLE_NAME);
                myDataBase = context.openOrCreateDatabase("Db",Context.MODE_WORLD_WRITEABLE, null);         //Opens database in writable mode.
                String titleString=null;
                String markString= null;
                int i;
                titleString = title.get(0)+",";
                markString = "?,";

                Log.d("**createDynamicDatabase", "in oncreate");

                for(i = 1; i < title.size() - 1; i++)
                {               
                    titleString += title.get(i);
                    titleString +=",";
                    markString += "?,";
                }

                titleString+= title.get(i);
                markString += "?";

                //System.out.println("Title String: "+titleString);
                //System.out.println("Mark String: "+markString);


                INSERT="insert into "+ TABLE_NAME + "("+titleString+")"+ "values" +"("+markString+")";
                System.out.println("Insert statement: "+INSERT);
                //System.out.println("Array size iiiiii::: "+array_vals.size());
                //this.insertStmt = this.myDataBase.compileStatement(INSERT);
                int s=0;

                while(s<array_vals.size()){

                System.out.println("Size of array1"+array_vals.size());
                        //System.out.println("Size of array"+title.size());
                int j=1;
                this.insertStmt = this.myDataBase.compileStatement(INSERT);
                for(int k =0;k< title.size();k++)
                {

                    //System.out.println("Value of column "+title+" is "+array_vals.get(k+s));
                    //System.out.println("PRINT S:"+array_vals.get(k+s));
                    System.out.println("BindString: insertStmt.bindString("+j+","+ array_vals.get(k+s)+")");
                    insertStmt.bindString(j, array_vals.get(k+s));



                    j++;
                }

                s+=title.size();

                }
                insertStmt.executeInsert();
            }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100008755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档