我尝试使用列" URL"更新SQLite数据库。具有特定价值。旧的网址格式为" http://www.blz.nl/voster/boek/9789056628512"新的是" http://www.blz.nl/voster/boekwinkel/zoeken/?q=9780789327505&oa=true&searchin=taal&taal=dut"。
我想要做的是将网址替换为新格式,但保留' Q'的值。来自旧网址的param。所有列的最快/最佳方法是什么?我不知道如何使用SQL查询来解决这个问题。
答案 0 :(得分:0)
你想要SQL吗?
基本上,您在表中读取,搜索关键字,替换它,然后将数据写回数据库。
假设您的数据库名称是“mydatabase.db”,您的代码将看起来像这样(未经测试):
using Devart.Data.SQLite;
public void Convert()
{
using (var conn = new SQLiteConnection("DataSource=mydatabase.db"))
{
var boek = "//boek//";
var boekwinkel = "//boekwinkel//";
var boek_len = boek.Length;
var table = new DataTable();
var sqlCmd = "SELECT * FROM TableName1;";
conn.Open();
// Read the data into the DataTable:
using (var cmd = new SQLiteCommand(sqlCmd, conn))
{
// The DataTable is ReadOnly!
table.Load(cmd.ExecuteReader());
}
// Use a new SQLiteCommand to write the data.
using (var cmd = new SQLiteCommand("UPDATE TableName1 SET URL=@NEW_URL WHERE URL=@URL;", conn))
{
cmd.Parameters.Add("@URL", SQLiteType.Text, 20);
cmd.Parameters.Add("@NEW_URL", SQLiteType.Text, 20);
foreach (DataRow row in table.Rows)
{
var url = String.Format("{0}", row["URL"]).Trim();
cmd.Parameters["@URL"].SQLiteValue = row["URL"];
if (!String.IsNullOrEmpty(url))
{
var index = url.IndexOf(boek);
if (-1 < index)
{
var first = url.Substring(0, index);
var middle = boekwinkel;
var last = url.Substring(index + boek_len);
var new_url = String.Format("{0}{1}{2}&oa=true&searchin=taal&taal=dut", first, middle, last);
// Place a break point after String.Format.
// Make sure the information being written is correct.
cmd.Parameters["@NEW_URL"].SQLiteValue = new_url;
cmd.ExecuteNonQuery();
}
}
}
}
conn.Close();
}
}