使用示例:
package cn.hackcoder.beautyreader.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; /** * Created by hackcoder on 15-1-25. */ public class DataBaseHelper extends SQLiteOpenHelper { private static final String dbName = "sample.db"; private static int dbVersion = 1; public DataBaseHelper(Context context) { super(context,dbName,null,dbVersion); } @Override public void onCreate(SQLiteDatabase db) { Log.d("===========","数据库初始化"); //建表 String sql = "create table if not exists tb_article(id integer primary key autoincrement,title varchar(50),content TEXT,url varchar(50),page integer)"; db.execSQL(sql); } /** * * @param db * @param oldVersion * @param newVersion */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
类源码:
package cn.hackcoder.beautyreader.service; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import java.util.ArrayList; import java.util.List; import cn.hackcoder.beautyreader.db.DataBaseHelper; import cn.hackcoder.beautyreader.model.Article; /** * Created by hackcoder on 15-1-25. */ public class ArticleService { private DataBaseHelper dataBaseHelper; private SQLiteDatabase readableDatabase; private SQLiteDatabase writableDatabase; public ArticleService(Context context) { dataBaseHelper = new DataBaseHelper(context); } public void add(Article article) { String sql = "insert into tb_article(id,title,content,url,page) values(?,?,?,?,?)"; getReadableDatabase().execSQL(sql, new Object[]{null, article.getTitle(), article.getContent(), article.getUrl(), article.getPage()}); } public void delete(int id) { String sql = "delete from tb_article where id =?"; getReadableDatabase().execSQL(sql, new Object[]{id}); } public void deleteAll() { String sql = "delete from tb_article"; getReadableDatabase().execSQL(sql,null); } public void update(Article article) { String sql = "update tb_article set title=?,content=?,url=?,page = ? where id =?"; getReadableDatabase().execSQL(sql, new Object[]{article.getTitle(), article.getContent(), article.getUrl(), article.getPage(), article.getId()}); } public void updateContentOfUrl(String url,String content){ String sql = "update tb_article set content=? where url =?"; getReadableDatabase().execSQL(sql, new Object[]{content,url}); } public Article find(int id) { Article article = new Article(); String sql = "select id,title,content,url,page from tb_article where id = ?"; Cursor cursor = getReadableDatabase().rawQuery(sql, new String[]{String.valueOf(id)}); if (cursor.moveToNext()) { article.setId(id); article.setTitle(cursor.getString(cursor.getColumnIndex("title"))); article.setContent(cursor.getString(cursor.getColumnIndex("content"))); article.setUrl(cursor.getString(cursor.getColumnIndex("url"))); article.setPage(cursor.getInt(cursor.getColumnIndex("page"))); cursor.close(); return article; } cursor.close(); return null; } public List<Article> findByUrl(String url) { List<Article> articles = new ArrayList<Article>(); String sql = "select id,title,content,url,page from tb_article where url = ?"; Cursor cursor = getReadableDatabase().rawQuery(sql, new String[]{url}); while (cursor.moveToNext()) { Article article = new Article(); article.setId(cursor.getInt(cursor.getColumnIndex("id"))); article.setTitle(cursor.getString(cursor.getColumnIndex("title"))); article.setContent(cursor.getString(cursor.getColumnIndex("content"))); article.setUrl(cursor.getString(cursor.getColumnIndex("url"))); article.setPage(cursor.getInt(cursor.getColumnIndex("page"))); articles.add(article); } cursor.close(); return articles; } public int getCountOfPage(int page){ String sql = "select count(*) from tb_article where page = ?"; Cursor cursor = getReadableDatabase().rawQuery(sql, new String[]{String.valueOf(page)}); cursor.moveToFirst(); int count = cursor.getInt(0); cursor.close(); return count; } public List<Article> getArticlesOfPage(int curPage){ List<Article> articles = new ArrayList<Article>(); String sql = "select id,title,content,url,page from tb_article where page = ?"; Cursor cursor = getReadableDatabase().rawQuery(sql,new String[]{String.valueOf(curPage)}); while(cursor.moveToNext()){ Article article = new Article(); article.setId(cursor.getInt(cursor.getColumnIndex("id"))); article.setTitle(cursor.getString(cursor.getColumnIndex("title"))); article.setContent(cursor.getString(cursor.getColumnIndex("content"))); article.setUrl(cursor.getString(cursor.getColumnIndex("url"))); article.setPage(cursor.getInt(cursor.getColumnIndex("page"))); articles.add(article); } cursor.close(); return articles; } public int countOfSum() { String sql = "select count(*) from tb_article"; Cursor cursor = getReadableDatabase().rawQuery(sql, null); cursor.moveToFirst(); int count = cursor.getInt(0); cursor.close(); return count; } public List<Article> getArticles(int start, int pageSize) { List<Article> articles = new ArrayList<Article>(); String sql = "select id,title,content,url,page from tb_article limit ?,?"; Cursor cursor = getReadableDatabase().rawQuery(sql,new String[]{String.valueOf(start),String.valueOf(pageSize)}); while(cursor.moveToNext()){ Article article = new Article(); article.setId(cursor.getInt(cursor.getColumnIndex("id"))); article.setTitle(cursor.getString(cursor.getColumnIndex("title"))); article.setContent(cursor.getString(cursor.getColumnIndex("content"))); article.setUrl(cursor.getString(cursor.getColumnIndex("url"))); article.setPage(cursor.getInt(cursor.getColumnIndex("page"))); articles.add(article); } cursor.close(); return articles; } public void closeDB() { if (readableDatabase != null && readableDatabase.isOpen()) { readableDatabase.close(); } if (writableDatabase != null && writableDatabase.isOpen()) { writableDatabase.close(); } } public SQLiteDatabase getReadableDatabase() { return dataBaseHelper.getReadableDatabase(); } public SQLiteDatabase getWritableDatabase() { return dataBaseHelper.getWritableDatabase(); } }
htdbm用于操作由mod_authn_dbm提供的HTTP基本认证所使用的保存用户名和密码的DBM数据库文件。参见dbmmanage文档以获得这些DBM文件的更多信息。 语法 htdbm [ -TDBTYPE ] [ -c ] [ -m | -d | -p | -s ] [ -t ] [ -v ] [ -x ] filename username htdbm -b [ -TDBTYPE ] [
本文向大家介绍PHP独立Session数据库存储操作类分享,包括了PHP独立Session数据库存储操作类分享的使用技巧和注意事项,需要的朋友参考一下 直接上代码:
数据库的操作是每个phpweb框架的核心功能,我们提供数据库数据库的标准调用模式为: 你可以执行严格模式,也就是左边的那条线,Controller->Service->Dao->Model->DB,也可以走简单模式 Controller->Model->DB。个人根据自己的需求,走标准麻烦些(几乎所有的机构和系统都是这样^_^), 但是扩展性会好些,但是使用简单模式开发效率就会高些,性能会好些,但
这一小节是对数据库操作做一个简单的封装,不涉及复杂的事务操作等。 我选用了Sql2o作为底层数据库框架作为支持,它的简洁易用性让我刮目相看,后面我们也会写如何实现一个ORM框架。 /** * 数据库支持 * @author biezhi * */ public final class MarioDb { private static Sql2o sql2o = null;
本文向大家介绍ASP.NET数据库操作类实例,包括了ASP.NET数据库操作类实例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了ASP.NET数据库操作类。分享给大家供大家参考,具体如下: 更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net优化技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》
本文向大家介绍Python操作使用MySQL数据库的实例代码,包括了Python操作使用MySQL数据库的实例代码的使用技巧和注意事项,需要的朋友参考一下 Python 操作 MySQL 配置 win_64 Ubuntu14.04 Python3.x pip安装pymysql模块 直接使用pip安装 pip install pymysql win64上直接在cmd中执行 连接本地数据库 使用模块p