//--------------------------------------------------------------------------
//
// Copyright (c) BUSHUOSX. All rights reserved.
//
// File: SqliteDbManager.cs
//
// Version:1.0.0.0
//
// Datetime:
//
//---------------------------------------------------------------------------
/*
*
* 修改时间:20140829 211000
*
*
*
*
*/ using System;
using System.Configuration;
using System.Data.SQLite;
using System.Text;
using System.Text.RegularExpressions;
using System.IO; namespace BUSHUOSX.Helper
{ public sealed class SqliteDbManager
{
/// <summary>
/// 获取连接字符串中某项的值
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="itemName">项目</param>
/// <returns>项目值</returns>
private static string GetItemValueFromConnectionString(string connectionString, string itemName)
{
if (!connectionString.EndsWith(";"))
connectionString += ";"; // \s* 匹配0个或多个空白字符
// .*? 匹配0个或多个除 "\n" 之外的任何字符(?指尽可能少重复)
string regexStr = itemName + @"\s*=\s*(?<key>.*?);";
Regex r = new Regex(regexStr, RegexOptions.IgnoreCase);
Match mc = r.Match(connectionString);
return mc.Groups["key"].Value;
} /// <summary>
/// 创建System.Data.Sqlite数据库样式的连接字符串
/// </summary>
/// <param name="dbName">数据库名</param>
/// <param name="dbFileExtName">数据库文件扩展名</param>
/// <param name="dbPassword">数据库密码</param>
/// <param name="dbDirectory">数据库目录路径</param>
/// <returns>dbName为空时,返回空</returns>
public static string GenerateSqliteConnectionString(string dbFileName, string dbPassword = "", string dbDirectory = "")
{
if (string.IsNullOrEmpty(dbFileName))
{
return "";
} StringBuilder sb = new StringBuilder("Data Source=");
if (!string.IsNullOrEmpty(dbDirectory))
{
sb.Append(dbDirectory);
if (!dbDirectory.EndsWith("\\"))
sb.Append('\\');
} sb.Append(dbFileName).Append(';'); if (!string.IsNullOrEmpty(dbPassword))
sb.AppendFormat("Password={0};", dbPassword); return sb.ToString();
} /// <summary>
/// 尝试用sqliteConnectionString打开已存在的数据库,或者用sqliteConnectionString创建新的数据库
/// </summary>
/// <param name="dbFileName"></param>
/// <param name="dbPassword"></param>
/// <returns></returns>
public static bool OpenOrCreateSqliteDateBase(string dbFileName, string dbPassword)
{
return OpenOrCreateSqliteDateBase(GenerateSqliteConnectionString(dbFileName, dbPassword));
} /// <summary>
/// 尝试用sqliteConnectionString打开已存在的数据库,或者用sqliteConnectionString创建新的数据库
/// </summary>
/// <param name="sqliteConnectionString"></param>
/// <returns></returns>
public static bool OpenOrCreateSqliteDateBase(string sqliteConnectionString)
{
string dbFileName = GetItemValueFromConnectionString(sqliteConnectionString, "Data Source");
string dbPassword = GetItemValueFromConnectionString(sqliteConnectionString, "Password");
if (string.IsNullOrEmpty(dbFileName))
{
return false;
} try
{
if (!File.Exists(dbFileName))
{
//创建目录
var parent = Directory.GetParent(dbFileName);
if (null != parent && !parent.Exists)
{
Directory.CreateDirectory(parent.FullName);
}
//var fs = File.Create(dbName);
//fs.Close(); //创建数据库
SQLiteConnection sqlconn = new SQLiteConnection("Data Source=" + dbFileName);
sqlconn.Open();
//设置密码
if (!string.IsNullOrEmpty(dbPassword))
sqlconn.ChangePassword(dbPassword);
sqlconn.Close(); return true;
}
}
catch (SQLiteException e)
{
return false;
}
catch (Exception e)
{
return false;
} try
{
//尝试打开数据库
SQLiteConnection sqlconn = new SQLiteConnection(sqliteConnectionString);
sqlconn.Open();
sqlconn.Close();
}
catch (SQLiteException e)
{
return false;
} return true;
} public static bool TableExists(string sqliteConnectionString, string tableName)
{
bool result = false;
SQLiteConnection sqlconn = new SQLiteConnection(sqliteConnectionString);
try
{
SQLiteCommand scmd = new SQLiteCommand(sqlconn);
scmd.CommandText = string.Format(@"select count(*) from sqlite_master where type='table' and name='{0}'", tableName); //打开数据库
sqlconn.Open(); var v = scmd.ExecuteScalar();
if ( == Convert.ToInt32(v))
{
result = true;
}
}
catch (Exception)
{
//throw;
}
sqlconn.Close();
return result;
} public static bool DropTable(string sqliteConnectionString, string tableName)
{
return null != ExecuteNonQuery(sqliteConnectionString, string.Format("drop table if exists {0}", tableName));
} public static bool RenameTable(string sqliteConnectionString, string oldTableName, string newTableName)
{
return null != ExecuteNonQuery(sqliteConnectionString, string.Format("alter table {0} rename to {1}", oldTableName, newTableName));
} /// <summary>
/// 执行sql命令
/// </summary>
/// <param name="sqliteConnectionString"></param>
/// <param name="sql"></param>
/// <returns></returns>
public static object ExecuteNonQuery(string sqliteConnectionString, string sql)
{
object result = null; SQLiteConnection sqlconn = new SQLiteConnection(sqliteConnectionString);
try
{
//打开数据库
sqlconn.Open(); SQLiteCommand scmd = new SQLiteCommand(sqlconn);
scmd.CommandText = sql;
result = scmd.ExecuteNonQuery();
}
catch (Exception e)
{
//throw;
}
sqlconn.Close(); return result;
}
}
}

sqlite helper的更多相关文章

  1. SQLite Helper (C#) zt

    http://www.codeproject.com/Articles/746191/SQLite-Helper-Csharp This small class (SQLiteHelper.cs) i ...

  2. SQLite Helper (C#) z

    http://www.codeproject.com/Articles/746191/SQLite-Helper-Csharp Introduction I have written a small ...

  3. C#操作SQLite数据库

    SQLite介绍 SQLite is a software library that implements a self-contained, serverless, zero-configurati ...

  4. Android中多表的SQLite数据库(译)

    原文: Android SQLite Database with Multiple Tables 在上一篇教程Android SQLite Database Tutorial中,解释了如何在你的And ...

  5. C# SQLite 创建数据库的方法增删查改语法和命令

    SQLite介绍 SQLite是一个开源.免费的小型RDBMS(关系型数据库),能独立运行.无服务器.零配置.支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准. SQLite数据库官方主页 ...

  6. 安卓APP与智能硬件相结合的简易方案

    第1章 概  述 (作者小波QQ463431476) (来源http://blog.chinaaet.com/zhaocundang/p/5100017645博客) (来源   http://www. ...

  7. HRPlugin For Xcode发布(附源码地址)

    今天给大家介绍的这个插件,是我在IOS平台上开发以来,一些想法的集合体.因为本人时常感觉在开发过程中无论从GOOGLE资料查找和SQL数据库查询,正则表达式测试,SVN等,这些经常要做的操作中,耽误了 ...

  8. 使用Rxjava缓存请求

    最近,我尝试使用RxJava开发了一款闲时备份app.我必须承认,一旦你get到了正确的方式,RxJava几乎感觉就像作弊.一切看起来更简洁,多个请求能够被组合,且非常容易控制.通过在UI线程观察和在 ...

  9. Android Third Party Libraries and SDK's

    http://javatechig.com/Android/android-third-party-libraries-sdks Over past few years, the age of mob ...

随机推荐

  1. 实现websocket

    转:http://www.cnblogs.com/dolphinX/p/3462898.html 在之前的博客中提到过看到html5 的websocket后很感兴趣,终于可以摆脱长轮询(websock ...

  2. MyBatis(9)整合spring

    具体的感兴趣可以参考:MyBatis 此时此刻,没用的话不再多说了,直接开始代码工程吧! 整体的代码实现: 具体使用到的我们在进行细说 基本上理解一边就能会使用整合  准备工作:  db.proper ...

  3. ON_COMMAND ON_MESSAGE ON_NOTIFY区别与联系

    ON_COMMAND是菜单和工具栏项处理消息的宏 ON_MESSAGE是处理自定义消息的宏 ON_NOTIFY 是控件向其父窗口发送消息处理的宏 对这几个消息的理解要先了解一下Window消息的背景. ...

  4. kettle maven 配置

    <properties> <kettle.version>6.1.0.4-225</kettle.version> </properties> < ...

  5. Gradle Goodness: Profiling Information

    If we want to know more about how much time is spent in tasks we can use the --profile command-line ...

  6. 在windows service中启动类型“Automatic” 和 “Automatic (Delayed start)” 有何不同?

    问题: When installing Windows services there are two options for automatically starting a Windows serv ...

  7. vue入门学习示例

    鄙人一直是用angular框架的,所以顺便比较了一下. <!DOCTYPE html> <html lang="en"> <head> < ...

  8. 学习笔记 - Ford-Fulkerson & EK

    Ford-Fulkerson & EK - 学习笔记 之前网络流什么的快忘完了 老师讲课的时候一脸懵逼--开始系统复习,从最大流开始 标签:网络流-最大流 『预备』 首先复习了网络流的概念-- ...

  9. 在控制台中操作MYSQL数据库步骤以及一些小问题

    一直用Navicat来对MySQL数据库进行操作,今天突然想试试用DOS控制台来操作,特记录自己第一次使用经历,若有错误之处,还望大佬们指点. 首先打开控制台,win+R键,输入cmd,确定 输入my ...

  10. linux 第十天学习

    一.RAID 1.常见RAID (RAID 0.RAID1.RAID5.RAID10) 2.RAID 10 阵列添加 2.1.添加硬盘 2.2.查看系统加载 2.3.mdadm 命令添加RAID阵列 ...