SQLite 데이터베이스에 데이터 저장

4639 단어
데이터를 SQLite에 저장하려면 다음과 같은 두 가지 방법으로 SQLiteOpenHelper 클래스를 재작성해야 합니다.
public class MyDatabaseHelper extends SQLiteOpenHelper {
    private Context mContext;
    private String CREATE_BOOK="create table book(id integer primary key autoincrement,author text,price real,pages integer,name text)";
    private String CREATE_CATEGORY="create table category(id integer primary key autoincrement,category_name text,category_code integer)";
    public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext=context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_BOOK);
        db.execSQL(CREATE_CATEGORY);
        Toast.makeText(mContext,"       ",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists book");
        db.execSQL("drop table if exists category");
        onCreate(db);
    }
}

물론 삭제 수정 방법도 있다. getWritable Database()의 역할은 데이터베이스가 존재하지 않으면 하나를 만들고 존재하면 이미 만든 데이터베이스를 여는 것이다.
public class SQLiteActivity extends AppCompatActivity implements View.OnClickListener{

    private MyDatabaseHelper myDatabaseHelper;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite);
        myDatabaseHelper = new MyDatabaseHelper(SQLiteActivity.this,"Book.db",null,4);
        findViewById(R.id.createDB).setOnClickListener(this);
        findViewById(R.id.insert).setOnClickListener(this);
        findViewById(R.id.update).setOnClickListener(this);
        findViewById(R.id.delete).setOnClickListener(this);
        findViewById(R.id.query).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.createDB:
                myDatabaseHelper.getWritableDatabase();
                break;
            case R.id.insert:
                db= myDatabaseHelper.getWritableDatabase();
                db.execSQL("insert into book (name,author,pages,price) values (?,?,?,?)",new String[]{"     ","  ","600","50"});
                db.execSQL("insert into book (name,author,pages,price) values (?,?,?,?)",new String[]{"     ","  ","700","60"});
//                ContentValues values=new ContentValues();
//                values.put("name","     ");
//                values.put("author","  ");
//                values.put("pages","600");
//                values.put("price","50");
//                db.insert("book",null,values);
                Toast.makeText(SQLiteActivity.this,"      ",Toast.LENGTH_SHORT).show();
                break;
            case R.id.update:
                db= myDatabaseHelper.getWritableDatabase();
                db.execSQL("update book set price=? where name=?",new String[]{"30","     "});
                Toast.makeText(SQLiteActivity.this,"      ",Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                db= myDatabaseHelper.getWritableDatabase();
                db.execSQL("delete from book where pages>?",new String[]{"600"});
                Toast.makeText(SQLiteActivity.this,"      ",Toast.LENGTH_SHORT).show();
                break;
            case R.id.query:
                db= myDatabaseHelper.getWritableDatabase();
                Cursor cursor=db.rawQuery("select * from book",null);
                if (cursor.moveToFirst()){
                    do {
                        String name=cursor.getString(cursor.getColumnIndex("name"));
                        String author=cursor.getString(cursor.getColumnIndex("author"));
                        int pages=cursor.getInt(cursor.getColumnIndex("pages"));
                        double price=cursor.getDouble(cursor.getColumnIndex("price"));
                        Toast.makeText(SQLiteActivity.this,name+author+pages+price,Toast.LENGTH_SHORT).show();
                    }while (cursor.moveToNext());
                }
                Toast.makeText(SQLiteActivity.this,"      ",Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

좋은 웹페이지 즐겨찾기