Android SQLite数据库判断某张表是否存在不存在则创建的语句

本文介绍了一种在SQLite数据库中创建表并判断其是否已存在的方法,通过SQL语句和Java代码实现,避免了重复创建表的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值