如何检查表格是否已经存在

时间:2019-07-17 02:12:45

标签: android sqlite android-sqlite android-database

当我运行应用程序时,我希望我的应用程序自动创建数据库和表,但是在此代码中,我的应用程序仅创建了没有表的数据库

所以我在按下按钮时就做到了,那时还有以addBio函数执行的插入查询形式的数据,但是总是返回现有的表错误,因此如何处理< / p>

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "biodata.db";
    private static final String TABEL_NAME = "biodata";
    private static final int DATABASE_VERSION = 1;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String sql = "CREATE TABLE " + TABEL_NAME + " ( id INTEGER PRIMARY KEY AUTOINCREMENT, nama TEXT, lahir TEXT, kelamin TEXT, hobi TEXT, alamat TEXT);";
        sqLiteDatabase.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS '" + TABEL_NAME + "'");
        onCreate(db);
    }

    public void addBio (String bio) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(bio);
        Log.d("TAGINI", bio);
    }
}

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;

public class TambahActivity extends AppCompatActivity {

    private Button btnSimpan;
    private DatabaseHelper databaseHelper;
    private EditText nama, lahir, hobi, alamat, jk;
    private ArrayList<String> arrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tambah);

        databaseHelper = new DatabaseHelper(this);

        nama = findViewById(R.id.nama);
        lahir = findViewById(R.id.tanggal_lahir);
        jk = findViewById(R.id.jk);
        hobi = findViewById(R.id.hobi);
        alamat = findViewById(R.id.alamat);
        btnSimpan = findViewById(R.id.btnSimpan);

        btnSimpan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String sql = "INSERT INTO biodata (id, nama, lahir, kelamin, hobi, alamat) VALUES('', '" +
                        nama.getText().toString() +"','"+
                        lahir.getText().toString()+"','"+
                        jk.getText().toString()+"','"+
                        hobi.getText().toString()+"','"+
                        alamat.getText().toString()+"');";
                databaseHelper.addBio(sql);
            }
        });
    }
}

我希望当我运行该应用程序时,将自动在其中创建数据库和表

1 个答案:

答案 0 :(得分:1)

您不需要写插入sql。请改用db.insert

  btnSimpan.setOnClickListener(new View.OnClickListener() {
                 addBio(nama.getText().toString());
        });

在addBio

public void addBio (String name) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues insertValues = new ContentValues();
    insertValues.put("name", name);
    db.insert("biodata", null, insertValues);
 }