zoukankan      html  css  js  c++  java
  • Windows Phone(三)WP7版 " 记账本" 开发(使用SQLite数据库)

    用这个SQLite数据库太纠结了..., ( O(∩_∩)O~欢迎拍砖...) ,所有代码砍掉了try...catch之类的东东,是为了尽可能简约明朗的说明问题.

    第一部分: 遇到的问题 :

    1. 在前一篇(Windows Phone(二) WP7数据库连接(SQLite数据库))我大概介绍了SQLite的使用方法,但在自己使用SQLite开发这个记账本程序的时候却遇到了N多问题,比如最头疼的问题: 数据怎么从SQLite数据库中取出来? 是的,你会说 Community.CsharpSqlite.WP.dll不就提供了一个方法吗? ExecuteQuery<T>() 不就可以吗? 是的,是可以,你得这么做(代码如下): 这个dll提供的方法实在是少的可怜,仅仅只有这一个方法,可以.Tolist(), .ToArray().ToDictionary();

    复制代码
       public ....ExecuteQuery(...)
    {
    ..........
    IEnumerable<Account> query = cmd.ExecuteQuery<Account>(); return query.ToList(); ...........
    }

    public
    class Account { /// <summary> /// 编号 /// </summary> public int id { get; set; } /// <summary> /// 用户编号 /// </summary> public int userId { get; set; } ......... }
    复制代码

    觉得不麻烦吗? 好吧,如果你的User类也要用这个方法呢? 你需要把这个ExecuteQuery()这个方法抽出来通用,那么这里的cmd.ExecuteQuery<Account>()中Account怎么处理呢,替换为 Object ? 这是会报错的...这个地方我还不知道怎么处理???求路过的童鞋看看有什么好的解决方法,

    我想应该不只有这一个dll,然后我就去网上去淘啊,淘啊,淘...O(∩_∩)O哈哈~

    让我淘到了:Community.CsharpSqlite.SqlLiteClient.WP7.dll, Community.CsharpSqlite.WP7.dll ,神器啊....然后就杯具了...(最后提供下载)

    这俩dll提供了读取数据的方法,但是不知道为什么,dll中的方法,在运行时方法名字变了..例如: read()-->reading(),太纠结了...还有其它一些问题就不细说了.

    可能我下载的dll是被人家修改过的,如果哪位有更好的,求分享!!!,或者有解决方法也行...

    最后木有办法,只好继续使用 Community.CsharpSqlite.WP.dll 先提一个,也是不知道为什么,有些汉字输入会报错,例如,你输入'大米' 程序会报'大'引发异常...

    第二部分: 记账本开发

    1. 帮助类: SQLiteHelper

    (1) 设置连接字符串:

    public static SQLiteConnection myDB = null;

    private static string fileName = "AccountData.DB";

    //fileName="Version=3,uri=file:AccountData.sqlite";-->这个是Community.CsharpSqlite.SqlLiteClient.WP7.dll定义数据库文件的方式

    (2) 创建数据库:

    打开数据库,如果数据库不存在就创建这个数据,-->这个是自动的,

    问题来了: 如果数据库不存在,直接创建数据库,顺便创建表:(表Account),如果数据库存在,就不能再创建表了啊,但是这个打开和创建数据库是自动的

    的楼主直接在SQL语句中写" create table if not exists Account(....", 可以在这里判断一下表存在与否 TabbleIsExist()

    SELECT COUNT(*) FROM sqlite_master where type='table' and name='Account'

    表Account不存在我才创建;

           之前看到过创建数据库表自增字段的方式,发现好多都是错误的,正确的方式如下,并且表只能有一个主键.至少SQLite中是这样:

    CREATE TABLE Account(

                 [id] INTEGER PRIMARY KEY AUTOINCREMENT, --主键,自增

                 [userId] INTEGER NOT NULL ,

                 [itemID]INTEGER ,

                 [item] nvarchar NOT NULL , --数据类型大小写木有区别,但是大小写统一比较好

                 [cost] INTEGER ,

                 [costTime] datetime , --大小写木有区别

                 [mark] int NOT NULL --使用int也是可以的

    ) ;

    (3) ExecuteNonQuery()方法: 这个没有什么说的,很简单,后面的代码中都有...

       (4) GetListAccount()方法: 是我要说的重点,弄这个太纠结了....

    我们知道,WP7不支持DataTable,也木有其它的什么好东东可以承载数据,怎么办,只好直接使用 Account类来接收数据,

    返回一个List<Account>,这样数据就出来了..

    IEnumerable<Account> query = cmd.ExecuteQuery<Account>();

    return query.ToList();

    悲剧的很...以后凡是涉及到数据查询,读取数据的都只能在这个方法上面折腾了...也是因为如此,我才花费N多时间折腾其它方式,

    木有办法最后还是回到了这里...哎呦...

    以下是帮助类具体代码:

    SQLiteHelper
    复制代码
     public class SQLiteHelper
        {
           
             /*
         *  数据库名: AccountData.DB 
            建表:
            a用户表: User
            id(编号)          int  pk  uq
            name(用户名)      nvarchar
            CreateTime(时间)  datetime
    
            b消费表: Account
            id(编号)          int pk  uq
            userId(用户编号)  int
            itemId(消费项编号)int uq
            item(消费项)      nvarchar
            cost(消费金额)    int
            costTime(消费时间)dateTime
            mark(标志位1 生活必须,2 奢侈享受) int
         */
    
            #region 构造函数
            public SQLiteHelper()
            {
            }  
            #endregion
          
            /// <summary>
            /// 连接字符串  需要修改
            /// </summary>
            public static SQLiteConnection myDB = null; 
    
            private static string fileName = "AccountData.DB";//"Version=3,uri=file:AccountData.sqlite";
            
            //这里先定数据库,之后再修改为参数化...
            /// <summary>
            /// 创建数据库
            /// </summary>
            public static void CreateDB()
            {
                if(myDB==null)
                {
                    try
                    {
                        //@1 使用open()方法打开数据库,如果数据库不存在就创建此数据库
                        myDB = new SQLiteConnection(fileName);
                        myDB.Open();
                    }
                    catch (SQLiteException ex)
                    {
                        MessageBox.Show("错误信息: " + ex);
                    }
                    //@2 创建表(方法内部要判断表是否存在)
                    CreateTable();
                    return;
                }
                myDB.Open();
            }
    
            /// <summary>
            /// 创建数据表 
            /// </summary>
            private static void CreateTable()
            {
                string strSql = string.Empty;
                if(myDB!=null)
                {
                    try
                    {
                        //先判断表是否存在  也可以直接使用SQL语句
                        //create table if not exists tableword (id integer primary key AUTOINCREMENT, word text, desc blob)
                        //if (!TabbleIsExist("User"))
                        //{
                        //    strSql = "CREATE TABLE User ([id] INTEGER PRIMARY KEY AUTOINCREMENT,[name] nvarchar ,[createTime] datetime);"; 
                        //}
    
                        if (!TabbleIsExist("Account"))
                        {
                            strSql +=
                            "CREATE TABLE Account([id] INTEGER PRIMARY KEY AUTOINCREMENT,[userId] INTEGER NOT NULL ,[itemID]INTEGER ,[item] nvarchar NOT NULL ,[cost] INTEGER ,[costTime] datetime ,[mark] int NOT NULL) ;";
                        }
                        if(!string.IsNullOrEmpty(strSql))
                        {
                            int i = ExecuteNonQuery(strSql);
                        }
                    }
                    catch (SQLiteException ex)
                    {
                        MessageBox.Show("错误信息: "+ex.Message);
                    }
                }
    
            }
         
            /// <summary>
            /// 判断数据库中表是否存在
            /// </summary>
            /// <param name="tableName">表名,区分大小写</param>
            /// <returns></returns>
            private static bool TabbleIsExist(String tableName)
            {
                if(string.IsNullOrEmpty(tableName))
                {
                    return false;
                }
                if(myDB!=null)
                {
                    string strSql = "SELECT COUNT(*) FROM sqlite_master where type='table' and name='" + tableName + "'";
                    SQLiteCommand cmd = myDB.CreateCommand(strSql);                
                    if (Convert.ToInt32(cmd.ExecuteScalar()) >0)
                    {
                        return true;
                    }
                }
                return false;
            }
    
            /// <summary>
            /// 执行SQL语句 添加,修改,删除操作. 
            /// </summary>
            /// <param name="strSql"></param>
            /// <returns></returns>
            public static int ExecuteNonQuery(string strSql)
            {
                if (myDB != null)
                {
                    try
                    {
                        SQLiteCommand cmd = myDB.CreateCommand(strSql);
                        return cmd.ExecuteNonQuery();
                    }
                    catch (SQLiteException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                return 0;
            }
    
            /// <summary>
            /// 执行SQL语句  查询Account 专用
            /// </summary>
            /// <param name="strSql"></param>
            /// <returns></returns>
            public static List<Account> GetListAccount(string strSql)
            {
                if(myDB!=null)
                {
                    try
                    {
                        SQLiteCommand cmd = myDB.CreateCommand(strSql);
                        IEnumerable<Account> query = cmd.ExecuteQuery<Account>();
                        return query.ToList();
                    }
                    catch (SQLiteException ex)
                    {
                        MessageBox.Show("错误消息: " + ex);    
                    }
                }
                return null;
            }     
        }
    复制代码

    帮助类搞定了,其它的就是小case了: 下面就只提供截图了,具体实现简单得很,就不献丑了...

    2. 主界面: 3. 生活必须: 4. 奢侈享受:

       5. 详情页:

    .............................

      就这么多吧,其它的就不提供了.

    第三部分: 相关dll 和一个SQLite工具下载:

       下载地址: http://115.com/file/anlgefdc

    本文参考DBFocus的博客 地址: http://www.cnblogs.com/dbfocus/archive/2011/02/27/1966203.html

    原贴:http://www.cnblogs.com/ry123/archive/2012/06/20/2554427.html

  • 相关阅读:
    HDU 2767 Proving Equivalences(强连通缩点)
    HDU 3836 Equivalent Sets(强连通缩点)
    POJ 2762 Going from u to v or from v to u?(强连通+缩点+拓扑排序)
    织梦dedecms中自定义表单必填项的设置方法
    dedecms中调用隐藏栏目的方法
    去掉dedecms友情链接中的LI标签的方法
    Mysql修改端口号 织梦DedeCMS设置教程
    织梦DedeCMS文章标题自动增加长尾关键词的方法
    DEDECMS短标题标签调用与字数修改的方法
    dedecms批量替换文章中超链接的方法
  • 原文地址:https://www.cnblogs.com/zziss/p/2740861.html
Copyright © 2011-2022 走看看