1、可以在创建表之前判断,这样就不会重新创建, String sql = "create table if not exists yijian(id INTEGER PRIMARY KEY,title varchar,body varchar,phone INTEGER,date timestamp not null default(datetime('now','localtime')))"; private DB_sql db_sql;
2.
//判断表是否存在 public void exitDataBase(SQLiteDatabase db) { String sql = "select count(*) as c from sqlite_master where type ='table' and name ='" + dbTitle + "';"; Cursor cursor = db.rawQuery(sql, null); if (cursor.moveToNext()) { int count = cursor.getInt(0); if (count > 0) { System.out.println("数据表已经存在"); } else { onCreate(db); } } }
public class DB_sql extends SQLiteOpenHelper { public static final int versions = 1;//必须static final,不然下面的版本爆红 public SQLiteDatabase db; public static String sql, dbTitle; public int m = 0; //构造方法 public DB_sql(Context context, String dbName, String dbTitle, String sql) {//注意这两行传参 super(context, dbName, null, versions); this.sql = sql; this.dbTitle = dbTitle; } @Override public void onCreate(SQLiteDatabase db) { this.db = db; db.execSQL(sql);//创建数据库 } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { // sqLiteDatabase.execSQL(sql); } //判断表是否存在 public void exitDataBase(SQLiteDatabase db) { String sql = "select count(*) as c from sqlite_master where type ='table' and name ='" + dbTitle + "';"; Cursor cursor = db.rawQuery(sql, null); if (cursor.moveToNext()) { int count = cursor.getInt(0); if (count > 0) { System.out.println("数据表已经存在"); } else { onCreate(db); } } } }
使用案例:
public class YijianActivity extends AppCompatActivity { private String time; private String sql = "create table if not exists yijian(id INTEGER PRIMARY KEY,title varchar,body varchar,phone INTEGER,date timestamp not null default(datetime('now','localtime')))"; private DB_sql db_sql; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.drawerlayout); ImageView btn_title = findViewById(R.id.btn_title); btn_title.setImageResource(R.drawable.ic_stat_name); btn_title.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); Button button_forward = findViewById(R.id.button_forward); button_forward.setText("我的意见"); button_forward.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(YijianActivity.this, MyYijianActivity.class)); } }); db_sql = new DB_sql(this, "Yijian", "yijian", sql); SQLiteDatabase db = db_sql.getReadableDatabase();//记住此处要用.getReadableDatabase(),因为先查一下 db_sql.exitDataBase(db); TextView tv_title = findViewById(R.id.text_title); tv_title.setText("意见反馈"); final EditText et_title = findViewById(R.id.et_title); final EditText et_title_body = findViewById(R.id.et_title_body); final EditText et_phone = findViewById(R.id.et_phone); Button btn_commit = findViewById(R.id.btn_commit); btn_commit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String title = et_title.getText().toString().trim(); String body = et_title_body.getText().toString().trim(); String phone = et_phone.getText().toString().trim(); if (TextUtils.isEmpty(title) || TextUtils.isEmpty(body) || TextUtils.isEmpty(phone)) { Toast.makeText(YijianActivity.this, "输入框不允许有空,请补全!", Toast.LENGTH_SHORT).show(); } else { // 获得写模式的数据仓库 SQLiteDatabase db = db_sql.getWritableDatabase(); //创建一个带值的表,它们的列名是keys ContentValues values = new ContentValues(); values.put("title", title);//将数据添加到ContentValues对象中 values.put("body", body); values.put("phone", phone); //插入新的行,返回新行的主键值 db.insert("yijian", null, values); Toast.makeText(YijianActivity.this, "提交成功!", Toast.LENGTH_SHORT).show(); db.close();//注意这一行 et_title.setText(""); et_phone.setText(""); et_title_body.setText(""); } } }); } }
3、
Cursor cursor = db.rawQuery("select name from sqlite_master where type='table';", null); while(cursor.moveToNext()){ //遍历出表名 String name = cursor.getString(0); Log.i("System.out", name); }
Android SQLite数据库判断某张表是否存在不存在则创建的语句
最新推荐文章于 2022-11-08 08:34:59 发布