在公司用着SQL Server
然后学了这个SQLite以后感觉真的是太方便了有没有 这么小的体积 一样的语法
先贴代码 这一次认真注释
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Source Code Pro';font-size:12.8pt;"><span style="color:#cc7832;">package </span>com.example.admin.sql<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#cc7832;">import </span>android.content.ContentValues<span style="color:#cc7832;">; </span><span style="color:#cc7832;">import </span>android.database.Cursor<span style="color:#cc7832;">; </span><span style="color:#cc7832;">import </span>android.database.sqlite.SQLiteDatabase<span style="color:#cc7832;">; </span><span style="color:#cc7832;">import </span>android.os.Bundle<span style="color:#cc7832;">; </span><span style="color:#cc7832;">import </span>android.support.v7.app.AppCompatActivity<span style="color:#cc7832;">; </span><span style="color:#cc7832;">import </span>android.util.Log<span style="color:#cc7832;">; </span><span style="color:#cc7832;">import </span>android.view.View<span style="color:#cc7832;">; </span><span style="color:#cc7832;">import </span>android.widget.Button<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#cc7832;">public class </span>MainActivity <span style="color:#cc7832;">extends </span>AppCompatActivity { <span style="color:#04da0a;">/* </span><span style="color:#04da0a;"> </span><span style="color:#04da0a;font-family:'宋体';">定义一个数据库类</span><span style="color:#04da0a;"> </span><span style="color:#04da0a;"> */ </span><span style="color:#04da0a;"> </span><span style="color:#cc7832;">private </span>MyDatabaseHelper <span style="color:#9876aa;">dbHelper</span><span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#bbb529;">@Override </span><span style="color:#bbb529;"> </span><span style="color:#cc7832;">protected void </span><span style="color:#1830ff;">onCreate</span>(Bundle savedInstanceState) { <span style="color:#cc7832;">super</span>.onCreate(savedInstanceState)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>setContentView(R.layout.<span style="color:#9876aa;"><em>activity_main</em></span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>Button createdatabase= (Button) findViewById(R.id.<span style="color:#9876aa;"><em>Create_database</em></span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#9876aa;">dbHelper</span>=<span style="color:#cc7832;">new </span>MyDatabaseHelper(<span style="color:#cc7832;">this,</span><span style="color:#ff7763;">"BookStore.db"</span><span style="color:#cc7832;">,null,</span><span style="color:#6897bb;">2</span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>createdatabase.setOnClickListener(<span style="color:#cc7832;">new </span>View.OnClickListener() { <span style="color:#bbb529;">@Override </span><span style="color:#bbb529;"> </span><span style="color:#cc7832;">public void </span><span style="color:#1830ff;">onClick</span>(View v) { <span style="color:#04da0a;">/* </span><span style="color:#04da0a;"> </span><span style="color:#04da0a;font-family:'宋体';">点击时调用</span><span style="color:#04da0a;">getWritableDatabase</span><span style="color:#04da0a;font-family:'宋体';">()方法 然后回去寻找有没有</span><span style="color:#04da0a;">Book</span><span style="color:#04da0a;font-family:'宋体';">这个数据库 </span><span style="color:#04da0a;font-family:'宋体';"> 没有回去创建</span><span style="color:#04da0a;">bookshujuku1 </span><span style="color:#04da0a;font-family:'宋体';">使用的前面创建的</span><span style="color:#04da0a;">onCrea</span><span style="color:#04da0a;font-family:'宋体';">()中的方法 </span><span style="color:#04da0a;font-family:'宋体';"> 所以会弹出一个窗口显示 创建成功</span><span style="color:#04da0a;"> </span><span style="color:#04da0a;"> */ </span><span style="color:#04da0a;"> </span><span style="color:#9876aa;">dbHelper</span>.getWritableDatabase()<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>} })<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#04da0a;">/* </span><span style="color:#04da0a;"> </span><span style="color:#04da0a;font-family:'宋体';">添加数据</span><span style="color:#04da0a;"> </span><span style="color:#04da0a;"> */ </span><span style="color:#04da0a;"> </span>Button addData= (Button) findViewById(R.id.<span style="color:#9876aa;"><em>add_data</em></span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>addData.setOnClickListener(<span style="color:#cc7832;">new </span>View.OnClickListener() { <span style="color:#bbb529;">@Override </span><span style="color:#bbb529;"> </span><span style="color:#cc7832;">public void </span><span style="color:#1830ff;">onClick</span>(View v) { SQLiteDatabase db=<span style="color:#9876aa;">dbHelper</span>.getWritableDatabase()<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>ContentValues values=<span style="color:#cc7832;">new </span>ContentValues()<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#c8b909;">//</span><span style="color:#c8b909;font-family:'宋体';">开始组装第一条数据 </span><span style="color:#c8b909;font-family:'宋体';"> </span>values.put(<span style="color:#ff7763;">"name"</span><span style="color:#cc7832;">,</span><span style="color:#ff7763;">"The Da Vinci Code"</span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>values.put(<span style="color:#ff7763;">"author"</span><span style="color:#cc7832;">,</span><span style="color:#ff7763;">"Dan Brown"</span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>values.put(<span style="color:#ff7763;">"pages"</span><span style="color:#cc7832;">,</span><span style="color:#6897bb;">454</span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>values.put(<span style="color:#ff7763;">"price"</span><span style="color:#cc7832;">,</span><span style="color:#6897bb;">16.96</span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>db.insert(<span style="color:#ff7763;">"Book"</span><span style="color:#cc7832;">, null, </span>values)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>values.clear()<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>values.put(<span style="color:#ff7763;">"name"</span><span style="color:#cc7832;">,</span><span style="color:#ff7763;">"The Lost Symbol"</span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>values.put(<span style="color:#ff7763;">"author"</span><span style="color:#cc7832;">,</span><span style="color:#ff7763;">"Dan Brown"</span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>values.put(<span style="color:#ff7763;">"pages"</span><span style="color:#cc7832;">,</span><span style="color:#6897bb;">510</span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>values.put(<span style="color:#ff7763;">"price"</span><span style="color:#cc7832;">,</span><span style="color:#6897bb;">19.95</span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>db.insert(<span style="color:#ff7763;">"Book"</span><span style="color:#cc7832;">, null, </span>values)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#cc7832;"> </span>} })<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#04da0a;">/* </span><span style="color:#04da0a;"> </span><span style="color:#04da0a;font-family:'宋体';">用</span><span style="color:#04da0a;">SQL</span><span style="color:#04da0a;font-family:'宋体';">语言直接添加数据</span><span style="color:#04da0a;"> </span><span style="color:#04da0a;"> */ </span><span style="color:#04da0a;"> </span>Button add= (Button) findViewById(R.id.<span style="color:#9876aa;"><em>add</em></span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>add.setOnClickListener(<span style="color:#cc7832;">new </span>View.OnClickListener() { <span style="color:#bbb529;">@Override </span><span style="color:#bbb529;"> </span><span style="color:#cc7832;">public void </span><span style="color:#1830ff;">onClick</span>(View v) { SQLiteDatabase db=<span style="color:#9876aa;">dbHelper</span>.getWritableDatabase()<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>db.execSQL(<span style="color:#ff7763;">"insert into Book(name,author,pages,price) values(?,?,?,?)"</span><span style="color:#cc7832;">, </span><span style="color:#cc7832;"> new </span>String[]{<span style="color:#ff7763;">"I love this world"</span><span style="color:#cc7832;">, </span><span style="color:#ff7763;">"yhluo"</span><span style="color:#cc7832;">, </span><span style="color:#ff7763;">"500"</span><span style="color:#cc7832;">, </span><span style="color:#ff7763;">"13.25"</span>})<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>db.execSQL(<span style="color:#ff7763;">"insert into Book(name,author,pages,price) values(?,?,?,?)"</span><span style="color:#cc7832;">, </span><span style="color:#cc7832;"> new </span>String[]{<span style="color:#ff7763;">"</span><span style="color:#ff7763;font-family:'宋体';">我是谁</span><span style="color:#ff7763;">"</span><span style="color:#cc7832;">, </span><span style="color:#ff7763;">"</span><span style="color:#ff7763;font-family:'宋体';">成龙</span><span style="color:#ff7763;">"</span><span style="color:#cc7832;">, </span><span style="color:#ff7763;">"125"</span><span style="color:#cc7832;">, </span><span style="color:#ff7763;">"13.25"</span>})<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#cc7832;"> </span>} })<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#cc7832;"> </span><span style="color:#04da0a;">/* </span><span style="color:#04da0a;"> </span><span style="color:#04da0a;font-family:'宋体';">更新数据</span><span style="color:#04da0a;"> </span><span style="color:#04da0a;"> */ </span><span style="color:#04da0a;"> </span>Button updateDate= (Button) findViewById(R.id.<span style="color:#9876aa;"><em>update_data</em></span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>updateDate.setOnClickListener(<span style="color:#cc7832;">new </span>View.OnClickListener() { <span style="color:#bbb529;">@Override </span><span style="color:#bbb529;"> </span><span style="color:#cc7832;">public void </span><span style="color:#1830ff;">onClick</span>(View v) { SQLiteDatabase db=<span style="color:#9876aa;">dbHelper</span>.getWritableDatabase()<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>ContentValues values=<span style="color:#cc7832;">new </span>ContentValues()<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>values.put(<span style="color:#ff7763;">"price"</span><span style="color:#cc7832;">, </span><span style="color:#6897bb;">20.99</span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>db.update(<span style="color:#ff7763;">"Book"</span><span style="color:#cc7832;">, </span>values<span style="color:#cc7832;">, </span><span style="color:#ff7763;">"author=?"</span><span style="color:#cc7832;">, new </span>String[]{<span style="color:#ff7763;">"Dan Brown"</span>})<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>} })<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#04da0a;">/* </span><span style="color:#04da0a;"> SQL</span><span style="color:#04da0a;font-family:'宋体';">语句直接更新数据</span><span style="color:#04da0a;"> </span><span style="color:#04da0a;"> */ </span><span style="color:#04da0a;"> </span>Button update= (Button) findViewById(R.id.<span style="color:#9876aa;"><em>update</em></span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>update.setOnClickListener(<span style="color:#cc7832;">new </span>View.OnClickListener() { <span style="color:#bbb529;">@Override </span><span style="color:#bbb529;"> </span><span style="color:#cc7832;">public void </span><span style="color:#1830ff;">onClick</span>(View v) { SQLiteDatabase db=<span style="color:#9876aa;">dbHelper</span>.getWritableDatabase()<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>db.execSQL(<span style="color:#ff7763;">"update Book set price=? where name=?"</span><span style="color:#cc7832;">, new </span>String[]{<span style="color:#ff7763;">"100.00"</span><span style="color:#cc7832;">, </span><span style="color:#ff7763;">"I love this world"</span>})<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>} })<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#04da0a;">/* </span><span style="color:#04da0a;"> </span><span style="color:#04da0a;font-family:'宋体';">删除数据</span><span style="color:#04da0a;"> </span><span style="color:#04da0a;"> */ </span><span style="color:#04da0a;"> </span>Button deleteDate= (Button) findViewById(R.id.<span style="color:#9876aa;"><em>delete_data</em></span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>deleteDate.setOnClickListener(<span style="color:#cc7832;">new </span>View.OnClickListener() { <span style="color:#bbb529;">@Override </span><span style="color:#bbb529;"> </span><span style="color:#cc7832;">public void </span><span style="color:#1830ff;">onClick</span>(View v) { SQLiteDatabase db=<span style="color:#9876aa;">dbHelper</span>.getWritableDatabase()<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>db.delete(<span style="color:#ff7763;">"Book"</span><span style="color:#cc7832;">, </span><span style="color:#ff7763;">"pages>?"</span><span style="color:#cc7832;">, new </span>String[]{<span style="color:#ff7763;">"500"</span>})<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#cc7832;"> </span>} })<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#04da0a;">/* </span><span style="color:#04da0a;"> SQL</span><span style="color:#04da0a;font-family:'宋体';">语句直接删除数据</span><span style="color:#04da0a;"> </span><span style="color:#04da0a;"> */ </span><span style="color:#04da0a;"> </span>Button delete= (Button) findViewById(R.id.<span style="color:#9876aa;"><em>delete</em></span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>delete.setOnClickListener(<span style="color:#cc7832;">new </span>View.OnClickListener() { <span style="color:#bbb529;">@Override </span><span style="color:#bbb529;"> </span><span style="color:#cc7832;">public void </span><span style="color:#1830ff;">onClick</span>(View v) { SQLiteDatabase db=<span style="color:#9876aa;">dbHelper</span>.getWritableDatabase()<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>db.execSQL(<span style="color:#ff7763;">"delete from Book where name=?"</span><span style="color:#cc7832;">, new </span>String[]{<span style="color:#ff7763;">"</span><span style="color:#ff7763;font-family:'宋体';">我是谁</span><span style="color:#ff7763;">"</span>})<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>} })<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#04da0a;">/* </span><span style="color:#04da0a;"> </span><span style="color:#04da0a;font-family:'宋体';">查询数据</span><span style="color:#04da0a;"> </span><span style="color:#04da0a;"> */ </span><span style="color:#04da0a;"> </span>Button queryData= (Button) findViewById(R.id.<span style="color:#9876aa;"><em>query_data</em></span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>queryData.setOnClickListener(<span style="color:#cc7832;">new </span>View.OnClickListener() { <span style="color:#bbb529;">@Override </span><span style="color:#bbb529;"> </span><span style="color:#cc7832;">public void </span><span style="color:#1830ff;">onClick</span>(View v) { SQLiteDatabase db=<span style="color:#9876aa;">dbHelper</span>.getWritableDatabase()<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>Cursor cursor=db.query(<span style="color:#ff7763;">"Book"</span><span style="color:#cc7832;">,null,null,null,null,null,null</span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> if</span>(cursor.moveToFirst()){ <span style="color:#cc7832;">do </span>{ <span style="color:#c8b909;">//</span><span style="color:#c8b909;font-family:'宋体';">遍历所有</span><span style="color:#c8b909;">Cursor</span><span style="color:#c8b909;font-family:'宋体';">对象,取出数据并打印 </span><span style="color:#c8b909;font-family:'宋体';"> </span>String name=cursor.getString(cursor.getColumnIndex(<span style="color:#ff7763;">"name"</span>))<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>String author=cursor.getString(cursor.getColumnIndex(<span style="color:#ff7763;">"author"</span>))<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> int </span>pages=cursor.getInt(cursor.getColumnIndex(<span style="color:#ff7763;">"pages"</span>))<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> double </span>price=cursor.getDouble(cursor.getColumnIndex(<span style="color:#ff7763;">"price"</span>))<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>Log.<span style="font-style:italic;">d</span>(<span style="color:#ff7763;">"MainActivity"</span><span style="color:#cc7832;">,</span><span style="color:#ff7763;">"book name is "</span>+name)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>Log.<span style="font-style:italic;">d</span>(<span style="color:#ff7763;">"MainActivity"</span><span style="color:#cc7832;">,</span><span style="color:#ff7763;">"book author is "</span>+author)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>Log.<span style="font-style:italic;">d</span>(<span style="color:#ff7763;">"MainActivity"</span><span style="color:#cc7832;">,</span><span style="color:#ff7763;">"book pages is "</span>+pages)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>Log.<span style="font-style:italic;">d</span>(<span style="color:#ff7763;">"MainActivity"</span><span style="color:#cc7832;">,</span><span style="color:#ff7763;">"book price is "</span>+price)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>}<span style="color:#cc7832;">while </span>(cursor.moveToNext())<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#cc7832;"> </span>} cursor.close()<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>} })<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#04da0a;">/* </span><span style="color:#04da0a;"> </span><span style="color:#04da0a;font-family:'宋体';">利用</span><span style="color:#04da0a;">SQL</span><span style="color:#04da0a;font-family:'宋体';">语句直接查询</span><span style="color:#04da0a;"> </span><span style="color:#04da0a;"> */ </span><span style="color:#04da0a;"> </span>Button query= (Button) findViewById(R.id.<span style="color:#9876aa;"><em>query</em></span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>query.setOnClickListener(<span style="color:#cc7832;">new </span>View.OnClickListener() { <span style="color:#bbb529;">@Override </span><span style="color:#bbb529;"> </span><span style="color:#cc7832;">public void </span><span style="color:#1830ff;">onClick</span>(View v) { SQLiteDatabase db=<span style="color:#9876aa;">dbHelper</span>.getWritableDatabase()<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>db.rawQuery(<span style="color:#ff7763;">"select * from Book"</span><span style="color:#cc7832;">,null</span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>} })<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>} }还是自己做的各种颜色看起来比较醒目
然后是SQLiteOpenHelper代码
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Source Code Pro';font-size:12.8pt;"><span style="color:#cc7832;">package </span>com.example.admin.sql<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#cc7832;">import </span>android.content.Context<span style="color:#cc7832;">; </span><span style="color:#cc7832;">import </span>android.database.sqlite.SQLiteDatabase<span style="color:#cc7832;">; </span><span style="color:#cc7832;">import </span>android.database.sqlite.SQLiteOpenHelper<span style="color:#cc7832;">; </span><span style="color:#cc7832;">import </span>android.widget.Toast<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#04da0a;"><strong>/** </strong></span><span style="color:#04da0a;"><strong> * Created by admin on 2016/3/14. </strong></span><span style="color:#04da0a;"><strong> */ </strong></span><span style="color:#cc7832;">public class </span>MyDatabaseHelper <span style="color:#cc7832;">extends </span>SQLiteOpenHelper { <span style="color:#04da0a;"><strong>/** </strong></span><span style="color:#04da0a;"><strong> * </strong></span><span style="color:#04da0a;font-family:'宋体';"><strong>定义一个数据库 </strong></span><span style="color:#04da0a;font-family:'宋体';"><strong> </strong></span><span style="color:#04da0a;"><strong>* SQL </strong></span><span style="color:#04da0a;"><strong> * </strong></span><span style="color:#04da0a;font-family:'宋体';"><strong>创建</strong></span><span style="color:#04da0a;"><strong>id</strong></span><span style="color:#04da0a;font-family:'宋体';"><strong>为主键包含作者、价格、页码、书名的</strong></span><span style="color:#04da0a;"><strong>table </strong></span><span style="color:#04da0a;"><strong> */ </strong></span><span style="color:#04da0a;"><strong> </strong></span><span style="color:#cc7832;">public static final </span>String <span style="color:#9876aa;"><em>CREATE_BOOK</em></span>=<span style="color:#ff7763;">"create table Book("</span>+<span style="color:#ff7763;">"id integer primary key autoincrement,"</span>+ <span style="color:#ff7763;">"author text,"</span>+<span style="color:#ff7763;">"price real,"</span>+<span style="color:#ff7763;">"pages integer,"</span>+<span style="color:#ff7763;">"name text)"</span><span style="color:#cc7832;">; </span><span style="color:#cc7832;"> public static final </span>String <span style="color:#9876aa;"><em>CREATE_CATEGORY</em></span>=<span style="color:#ff7763;">"create table Category("</span>+<span style="color:#ff7763;">"id integer primary key autoincrement,"</span>+ <span style="color:#ff7763;">"category_name text,"</span>+ <span style="color:#ff7763;">"category_code integer)"</span><span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#cc7832;"> private </span>Context <span style="color:#9876aa;">mContext</span><span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#04da0a;">/* </span><span style="color:#04da0a;"> </span><span style="color:#04da0a;font-family:'宋体';">四个参数分别是 上下文</span><span style="color:#04da0a;">/ </span><span style="color:#04da0a;font-family:'宋体';">数据库名</span><span style="color:#04da0a;">/ </span><span style="color:#04da0a;font-family:'宋体';">表示允许在查询时返回一个自定义的</span><span style="color:#04da0a;">Cursor/ </span><span style="color:#04da0a;font-family:'宋体';">表示版本号</span><span style="color:#04da0a;"> </span><span style="color:#04da0a;"> </span><span style="color:#04da0a;"> */ </span><span style="color:#04da0a;"> </span><span style="color:#cc7832;">public </span>MyDatabaseHelper(Context context<span style="color:#cc7832;">, </span>String name<span style="color:#cc7832;">, </span>SQLiteDatabase.CursorFactory factory<span style="color:#cc7832;">, int </span>version) { <span style="color:#cc7832;">super</span>(context<span style="color:#cc7832;">, </span>name<span style="color:#cc7832;">, </span>factory<span style="color:#cc7832;">, </span>version)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span><span style="color:#9876aa;">mContext</span>=context<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>} <span style="color:#bbb529;">@Override </span><span style="color:#bbb529;"> </span><span style="color:#cc7832;">public void </span><span style="color:#1830ff;">onCreate</span>(SQLiteDatabase db) { db.execSQL(<span style="color:#9876aa;"><em>CREATE_BOOK</em></span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>db.execSQL(<span style="color:#9876aa;"><em>CREATE_CATEGORY</em></span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>Toast.<span style="font-style:italic;">makeText</span>(<span style="color:#9876aa;">mContext</span><span style="color:#cc7832;">,</span><span style="color:#ff7763;">"Create Succeeded"</span><span style="color:#cc7832;">,</span>Toast.<span style="color:#9876aa;"><em>LENGTH_SHORT</em></span>).show()<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>} <span style="color:#bbb529;">@Override </span><span style="color:#bbb529;"> </span><span style="color:#cc7832;">public void </span><span style="color:#1830ff;">onUpgrade</span>(SQLiteDatabase db<span style="color:#cc7832;">, int </span>oldVersion<span style="color:#cc7832;">, int </span>newVersion) { db.execSQL(<span style="color:#ff7763;">"drop table if exists Book"</span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>db.execSQL(<span style="color:#ff7763;">"drop table if exists Category"</span>)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>onCreate(db)<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>} }
还有怎么查询数据库的方法
<img src="http://img.blog.csdn.net/20160314164927026?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
最后当然是UI啦
<img src="http://img.blog.csdn.net/20160314164631973?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
</pre><pre name="code" class="java">其实对我来说习惯了用SQLSever感觉直接用Sql来描述更容易一些