android sqlLite使用
来源:互联网 发布:linq.js where. First 编辑:程序博客网 时间:2024/05/22 18:56
1.SQLiteDatabase初步使用
- package com.example.sqlitedatabasetest;
-
- import android.support.v7.app.ActionBarActivity;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
-
- public class MainActivity extends ActionBarActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
-
- SQLiteDatabase db = openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
- db.execSQL("drop table if exists person");
-
-
- db.execSQL("create table person (_id integer primary key autoincrement, name varchar, age smallint)");
-
-
- Person person = new Person();
- person.name = "Jianan";
- person.age = 25;
-
-
- db.execSQL("insert into person values (null, ?, ?)", new Object[]{person.name, person.age});
-
-
- person.name = "Luohuijuan";
- person.age = 47;
-
-
- ContentValues cv = new ContentValues();
- cv.put("name", person.name);
- cv.put("age", person.age);
-
-
- db.insert("person", null, cv);
-
-
- cv = new ContentValues();
- cv.put("age", 35);
-
-
- db.update("person", cv, "name = ?", new String[]{"Jianan"});
-
-
- Cursor c = db.rawQuery("select * from person where age >= ?", new String[]{"33"});
- while(c.moveToNext()){
- int _id = c.getInt(c.getColumnIndex("_id"));
- String name = c.getString(c.getColumnIndex("name"));
- int age = c.getInt(c.getColumnIndex("age"));
- Log.e("db", "_id:" + _id + ",name:" + name + ",age:"+age);
- }
-
-
- c.close();
-
-
- db.delete("person", "age <= ?", new String[]{"35"});
-
-
- db.close();
-
-
-
-
- }
- }
-
-
-
-
-
0 0