lingxi22 2011-11-29 14:37
浏览 455
已采纳

使用jdbc连接sqlite3时,blob数据该怎么存入取出?

[code="java"]
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite://e:/hello.db");
Statement stmt = conn.createStatement();
[/code]

  • 写回答

1条回答 默认 最新

  • jiangguangxian 2011-11-30 08:27
    关注

    我只能给你两个c#的代码,给你一点参考:

    [code="java"]
    public static Dictionary GetBlob(string dirPath)
    {
    Dictionary dic = new Dictionary();

            List<string> lst = new List<string>();
            string strSQL = "select * from Resource";
            SQLiteCommand cmd = Connection.CreateCommand();
            cmd.CommandText = strSQL;
    
            try
            {
                if (Connection.State != ConnectionState.Open)
                    Connection.Open();
                SQLiteDataReader dataReader = cmd.ExecuteReader();
    
                while (dataReader.Read())
                {
                    string itemId = dataReader["ID"].ToString();
                    int nDataLen = (int)dataReader.GetBytes(1, 0, null, 0, 0);
                    byte[] pbyData = new byte[nDataLen];
                    dataReader.GetBytes(1, 0, pbyData, 0, nDataLen);
                    string fileName = string.Format(Path.Combine(dirPath, itemId + ".zip"));
                    dic[itemId] = fileName;
                    FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                    fs.Write(pbyData, 0, nDataLen);
                    fs.Close();
                }
                dataReader.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                cmd.Dispose();
                Connection.Close();
            }
            return dic;
        }
    
        public static int InsertBlob(string cmdText, byte[] data)
        {
            SQLiteCommand command = new SQLiteCommand();
            int nRowCount = 0;
            try
            {
                if (Connection.State != ConnectionState.Open)
                    Connection.Open();
                SQLiteParameter para = new SQLiteParameter("data", DbType.Binary);
                para.Value = data;
                command.Parameters.Add(para);
                command.Connection = Connection;
                command.CommandText = cmdText;
                command.CommandType = CommandType.Text;
                command.CommandTimeout = 30;
                nRowCount = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
            }
            finally
            {
                command.Dispose();
                Connection.Close();
            }
            return nRowCount;
    
        }
    

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?