使用jdbc连接sqlite3时,blob数据该怎么存入取出
1个回答
2016-11-18
展开全部
我只能给你两个c#的代码,给你一点参考:
public static Dictionary<string, string> GetBlob(string dirPath)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
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;
}
public static Dictionary<string, string> GetBlob(string dirPath)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询