解决windows phone中使用sqlite时报SQLiteClient.SQLiteException: unrecognized token错误

将要插入sqlite数据库的汉字转换成Chart后+3变成其它汉字后再插入(试了一下效果还可以),这个方法只是应急的。觉得把它转化成sqlite能识别的数据类型才是正道,留给以后再尝试吧。

Windows Phone简单操作sqlite数据库,引用省略,编码如下

using SQLiteClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Client
{
    public class VersionInfo
    {
        public VersionInfo()
        {
        }
        public VersionInfo(int Id, string Version,int ISUpdate)
        {
            id = Id;
            version = Version;
            IsUpdate = ISUpdate;
        }
        int _id;
        string _version;
        //是否升级(1:不升级,0:升级)
        private int isUpgrade;

        public int id
        {
            get { return _id; }
            set { _id = value; }
        }
        public string version
        {
            get { return _version; }
            set { _version = value; }
        }
        public int IsUpdate
        {
            get { return isUpgrade; }
            set { isUpgrade = value; }
        }
    }

    public static class SQLiteHelper
    {
        private static SQLiteConnection db = null;
        private static string fileName = "DB";
        public static void openDB()
        {
            if (db == null)
            {
                db = new SQLiteConnection(fileName);
                db.Open();
            }
        }

        public static void closeDB()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
        }

        public static bool isExistTable(string tablename)
        {
            bool flag = false;
            if (db != null)
            {
                try
                {
                    SQLiteCommand cmd = db.CreateCommand("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='" + tablename + "'");
                    object obj = cmd.ExecuteScalar();
                    string msg = string.Empty;
                    int iRet = Convert.ToInt32(obj);
                    if (iRet > 0)
                    {
                        flag = true;
                    }
                    return flag;
                }
                catch (SQLiteException ex)
                {
                    return flag;
                }
            }
            else
            {
                return flag;
            }
        }

        #region version

        public static void CreateVersionTable()
        {
            if (db != null)
            {
                try
                {
                    if (!isExistTable("VersionTable"))
                    {
                        SQLiteCommand cmd = db.CreateCommand("Create table VersionTable(id INTEGER primary key,version text,IsUpdate INTEGER)");
                        int i = cmd.ExecuteNonQuery();
                    }
                }
                catch (SQLiteException ex)
                {
                }
            }
        }

        public static VersionInfo seleceVersion()
        {
            if (db != null)
            {
                try
                {
                    SQLiteCommand cmd = db.CreateCommand("SELECT id,version,IsUpdate FROM VersionTable ");
                    var lst = cmd.ExecuteQuery<VersionInfo>();
                    if (lst.Count() == 0)
                    {
                        return null;
                    }
                    else
                    {
                        VersionInfo tt = lst.ToList()[0];
                        if (!string.IsNullOrEmpty(tt.version))
                        {
                            tt.version = StringDecoding(tt.version);
                        }
                        else
                        {
                            tt.version = "";
                        }
                        return tt;
                    }
                }
                catch (SQLiteException ex)
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        public static void insertVersionData(VersionInfo info)
        {
            if (db != null)
            {
                try
                {
                    deleteVersionData();//先删除后添加
                    SQLiteCommand cmd = db.CreateCommand("");
                    cmd.CommandText = " Insert into VersionTable (version,IsUpdate) values ('" + (!string.IsNullOrEmpty(info.version) ? StringEncoding(info.version) : "") + "','" + info.IsUpdate + "')";
                    int rec = cmd.ExecuteNonQuery();
                }
                catch (SQLiteException ex)
                {

                }
            }
        }

        public static void UpdateVersionData(VersionInfo info)
        {
            if (db != null)
            {
                try
                {
                    SQLiteCommand cmd = db.CreateCommand("");
                    cmd.CommandText = "update VersionTable set version='" + (!string.IsNullOrEmpty(info.version) ? StringEncoding(info.version) : "") + "',IsUpdate='" + info.IsUpdate + "'";
                    int rec = cmd.ExecuteNonQuery();
                }
                catch (SQLiteException ex)
                {
                    string exmessage = ex.ToString();
                }
            }
        }

        public static void deleteVersionData()
        {
            if (db != null)
            {
                try
                {
                    SQLiteCommand cmd = db.CreateCommand("");
                    cmd.CommandText = " delete from VersionTable ";
                    int rec = cmd.ExecuteNonQuery();
                }
                catch (SQLiteException ex)
                {

                }
            }
        }
        #endregion

        //加密,把 string 转换成 char 后用自己的方法加密解密。这样解决了汉字插入数据库时报错SQLiteClient.SQLiteException: unrecognized token: "'发言" at SQLiteClient.SQ
        public static string StringEncoding(string pwd)
        {
            char[] arrChar = pwd.ToCharArray();
            string strChar = "";
            for (int i = 0; i < arrChar.Length; i++)
            {
                arrChar[i] = Convert.ToChar(arrChar[i] + 3);
                strChar = strChar + arrChar[i].ToString();
            }
            return strChar;
        }

        //解密
        public static string StringDecoding(string pwd)
        {
            char[] arrChar = pwd.ToCharArray();
            string strChar = "";
            for (int i = 0; i < arrChar.Length; i++)
            {
                arrChar[i] = Convert.ToChar(arrChar[i] - 3);
                strChar = strChar + arrChar[i].ToString();
            }
            return strChar;
        }
    }
}

在项目中调用方法即可:

                SQLiteHelper.openDB();
                SQLiteHelper.CreateVersionTable();
                VersionInfo infoVersion = SQLiteHelper.seleceVersion();
                SQLiteHelper.closeDB();


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值