android sqlLite使用

来源:互联网 发布:linq.js where. First 编辑:程序博客网 时间:2024/05/22 18:56

1.SQLiteDatabase初步使用

[java] view plain copy
  1. package com.example.sqlitedatabasetest;  
  2.   
  3. import android.support.v7.app.ActionBarActivity;  
  4. import android.content.ContentValues;  
  5. import android.content.Context;  
  6. import android.database.Cursor;  
  7. import android.database.sqlite.SQLiteDatabase;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.view.Menu;  
  11. import android.view.MenuItem;  
  12.   
  13. public class MainActivity extends ActionBarActivity {  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.           
  20.         //  
  21.         SQLiteDatabase db = openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null); //内存的/data/data/<package name/databases>目录中  
  22.         db.execSQL("drop table if exists person");  
  23.           
  24.         //  
  25.         db.execSQL("create table person (_id integer primary key autoincrement, name varchar, age smallint)");  
  26.           
  27.         //  
  28.         Person person = new Person();  
  29.         person.name = "Jianan";  
  30.         person.age = 25;  
  31.           
  32.         //  
  33.         db.execSQL("insert into person values (null, ?, ?)"new Object[]{person.name, person.age});  
  34.           
  35.         //  
  36.         person.name = "Luohuijuan";  
  37.         person.age = 47;  
  38.           
  39.         //  
  40.         ContentValues cv = new ContentValues();  
  41.         cv.put("name", person.name);  
  42.         cv.put("age", person.age);  
  43.           
  44.         //  
  45.         db.insert("person"null, cv);  
  46.           
  47.         //  
  48.         cv = new ContentValues();  
  49.         cv.put("age"35);  
  50.           
  51.         //  
  52.         db.update("person", cv, "name = ?"new String[]{"Jianan"});  
  53.           
  54.         //  
  55.         Cursor c = db.rawQuery("select * from person where age >= ?"new String[]{"33"});  
  56.         while(c.moveToNext()){  
  57.             int _id = c.getInt(c.getColumnIndex("_id"));  
  58.             String name = c.getString(c.getColumnIndex("name"));  
  59.             int age = c.getInt(c.getColumnIndex("age"));  
  60.             Log.e("db""_id:" + _id + ",name:" + name + ",age:"+age);  
  61.         }  
  62.           
  63.         //  
  64.         c.close();  
  65.           
  66.         //  
  67.         db.delete("person""age <= ?"new String[]{"35"});  
  68.           
  69.         //  
  70.         db.close();  
  71.           
  72.         //  
  73. //      deleteDatabase("test.db");  
  74.           
  75.     }  
  76. }  
  77.   
  78. /* 
  79. _id:1,name:Jianan,age:35 
  80. _id:2,name:Luohuijuan,age:47 
  81. */  

0 0