Android数据库编程教程

Android数据库存储的位置在data/data/<项目文件夹>/databases/

1:创建Android数据库

Context.createDatabase(String name,int version ,int mode,CursorFactory factory)如果创建不成功则抛出FileNotFoundException异常

Java代码

<ol class="dp-xml">
<li class="alt"><span><span>SQLiteDatabase </span><span class="attribute">DataBase</span><span>=</span><span class="attribute-value">this</span><span>.openOrCreateDatabase("DataBase.db",     </span></span></li>
<li><span>                              MODE_PRIVATE,      </span></li>
<li class="alt"><span>                              new CursorFactory(){     </span></li>
<li><span>//创建新的数据库,名称Database,模式MODE_PRIVATE     </span></li>
<li class="alt"><span>    public Cursor newCursor(SQLiteDatabase db,     </span></li>
<li><span>                SQLiteCursorDriver masterQuery,      </span></li>
<li class="alt"><span>                String editTable,     </span></li>
<li><span>                SQLiteQuery query){     </span></li>
<li class="alt"><span>            return null;     </span></li>
<li><span>    }     </span></li>
<li class="alt"><span>});    </span></li>
<li><span> </span></li>
<li class="alt">
<span>SQLiteDatabase </span><span class="attribute">DataBase</span><span>=</span><span class="attribute-value">this</span><span>.openOrCreateDatabase("DataBase.db",  </span>
</li>
<li><span>               MODE_PRIVATE,   </span></li>
<li class="alt"><span>               new CursorFactory(){  </span></li>
<li><span>//创建新的数据库,名称Database,模式MODE_PRIVATE  </span></li>
<li class="alt"><span>    public Cursor newCursor(SQLiteDatabase db,  </span></li>
<li><span>       SQLiteCursorDriver masterQuery,   </span></li>
<li class="alt"><span>       String editTable,  </span></li>
<li><span>       SQLiteQuery query){  </span></li>
<li class="alt"><span>            return null;  </span></li>
<li><span>    }  </span></li>
<li class="alt"><span>}) </span></li>
</ol>

2:打开已经存在的数据库Context.openDatabase(String file,CursorFactory factory);如果我们要实现打开不存在的数据库就创建它,代码如下:

Java代码

<ol class="dp-xml">
<li class="alt"><span><span>//创建一个名为:DataBase的数据库,后缀为.db     </span></span></li>
<li>
<span>SQLiteDatabase </span><span class="attribute"><font color="#ff0000">DataBase</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">this</font></span><span>.openOrCreateDatabase("DateBase.db",     </span>
</li>
<li class="alt"><span>                               MODE_PRIVATE,      </span></li>
<li><span>                               null);     </span></li>
<li class="alt"><span>//也可以这样写     </span></li>
<li><span>try      </span></li>
<li class="alt"><span>{      </span></li>
<li><span>    //打开已经存在的数据库      </span></li>
<li class="alt">
<span>        </span><span class="attribute"><font color="#ff0000">db</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">this</font></span><span>.openDatabase("DateBase.db", null);           </span>
</li>
<li><span>}      </span></li>
<li class="alt"><span>catch (FileNotFoundException e)      </span></li>
<li><span>{      </span></li>
<li class="alt"><span>    try      </span></li>
<li><span>    {      </span></li>
<li class="alt"><span>    //建立新的数据库        </span></li>
<li><span>    }      </span></li>
<li class="alt"><span>    catch (FileNotFoundException e)      </span></li>
<li><span>    {      </span></li>
<li class="alt">
<span>    </span><span class="attribute"><font color="#ff0000">db</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">null</font></span><span>;      </span>
</li>
<li><span>    }      </span></li>
<li class="alt"><span>}     </span></li>
<li><span> </span></li>
<li class="alt"><span>//创建一个名为:DataBase的数据库,后缀为.db  </span></li>
<li>
<span>SQLiteDatabase </span><span class="attribute"><font color="#ff0000">DataBase</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">this</font></span><span>.openOrCreateDatabase("DateBase.db",  </span>
</li>
<li class="alt"><span>                MODE_PRIVATE,   </span></li>
<li><span>                null);  </span></li>
<li class="alt"><span>//也可以这样写  </span></li>
<li><span>try   </span></li>
<li class="alt"><span>{   </span></li>
<li><span> //打开已经存在的数据库   </span></li>
<li class="alt">
<span>        </span><span class="attribute"><font color="#ff0000">db</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">this</font></span><span>.openDatabase("DateBase.db", null);        </span>
</li>
<li><span>}   </span></li>
<li class="alt"><span>catch (FileNotFoundException e)   </span></li>
<li><span>{   </span></li>
<li class="alt"><span> try   </span></li>
<li><span> {   </span></li>
<li class="alt"><span> //建立新的数据库    </span></li>
<li><span> }   </span></li>
<li class="alt"><span> catch (FileNotFoundException e)   </span></li>
<li><span> {   </span></li>
<li class="alt">
<span> </span><span class="attribute"><font color="#ff0000">db</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">null</font></span><span>;   </span>
</li>
<li><span> }   </span></li>
<li class="alt"><span>}  </span></li>
</ol>

3:关闭数据库

Java代码

<ol class="dp-xml">
<li class="alt"><span><span>//千万不要忘记关闭数据库哦     </span></span></li>
<li><span>DataBase.close();     </span></li>
<li class="alt"><span> </span></li>
<li><span>//千万不要忘记关闭数据库哦  </span></li>
<li class="alt"><span>DataBase.close();  </span></li>
</ol>

4:删除指定数据库

Java代码

<ol class="dp-xml">
<li class="alt"><span><span>this.deleteDatabase("Database.db");    </span></span></li>
<li><span>this.deleteDatabase("Database.db"); </span></li>
</ol>

相关推荐