1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp3.TestActivity2" 11 android:orientation="vertical"> 12 13 <Button 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:text="初始化数据库" 17 android:onClick="bt1_OnClick"/> 18 19 <Button 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:text="升级数据库" 23 android:onClick="bt2_OnClick"/> 24 25 26 </LinearLayout>
.xml
1 package com.hanqi.testapp3; 2 3 import android.content.ContentValues; 4 import android.database.Cursor; 5 import android.database.sqlite.SQLiteDatabase; 6 import android.database.sqlite.SQLiteOpenHelper; 7 import android.support.v7.app.AppCompatActivity; 8 import android.os.Bundle; 9 import android.util.Log; 10 import android.view.View; 11 import android.widget.EditText; 12 import android.widget.Toast; 13 14 public class TestActivity2 extends AppCompatActivity { 15 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_test2); 21 22 } 23 24 //初始化数据库 25 public void bt1_OnClick(View v) 26 { 27 //使用工具类得到数据库对象 28 MyDBHelper myDBHelper=new MyDBHelper("test.db",2); 29 30 //得到连接 31 SQLiteDatabase sd=myDBHelper.getReadableDatabase(); 32 33 Toast.makeText(TestActivity2.this, "连接数据库成功", Toast.LENGTH_SHORT).show(); 34 35 //关闭连接 36 sd.close(); 37 } 38 39 //升级数据库 40 public void bt2_OnClick(View v) 41 { 42 //使用工具类得到数据库对象 43 MyDBHelper myDBHelper=new MyDBHelper("test.db",2); 44 45 //得到连接 46 SQLiteDatabase sd=myDBHelper.getWritableDatabase(); 47 48 Toast.makeText(TestActivity2.this, "连接数据库成功", Toast.LENGTH_SHORT).show(); 49 50 //关闭连接 51 sd.close(); 52 } 53 54 55 56 //实现SQLiteOpenHelper的内部类 57 class MyDBHelper extends SQLiteOpenHelper 58 { 59 //构造方法 60 public MyDBHelper(String dbname,int ver) 61 { 62 //显示调用父类的构造方法 63 //必须在第一行 64 super(TestActivity2.this,dbname,null,ver); 65 66 } 67 68 //创建和初始化数据库 69 @Override 70 public void onCreate(SQLiteDatabase db) { 71 72 //1.执行创建数据库的语句 73 String sql="CREATE TABLE t_user" + 74 "(_id INTEGER PRIMARY KEY " + 75 "AUTOINCREMENT NOT NULL," + 76 "name VARCHAR(20) NOT NULL," + 77 "sex CHAR(1),age INTEGER)"; 78 db.execSQL(sql); 79 80 Log.e("TAG","表创建成功"); 81 //2.执行初始化数据的语句,insert语句 82 ContentValues cv=new ContentValues(); 83 84 cv.put("name","张三"); 85 cv.put("sex","男"); 86 cv.put("age","20"); 87 88 //执行插入,返回主键 89 long l=db.insert("t_user",null,cv); 90 91 Log.e("TAG","初始化数据="+l); 92 } 93 94 //升级数据库 95 //触发条件:当版本号增大 96 @Override 97 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 98 99 //修改数据 100 if(newVersion==2) 101 { 102 ContentValues cv=new ContentValues(); 103 cv.put("name","李四"); 104 105 String sql="update t_user set name=‘李四‘ where _id=1 "; 106 107 String[] str={"1","18"}; 108 //调用db的更新方法 109 int i=db.update("t_user", cv, "_id=? and age>?",str); 110 111 Log.e("TAG","升级数据 数据条数="+i); 112 } 113 114 } 115 } 116 }
.java
时间: 2024-10-11 06:23:36