将布尔值恢复为真到假
本文关键字:布尔值 恢复 | 更新日期: 2023-09-27 18:36:22
在我的程序中,有一种方法可以备份如下:
INSERT INTO `vente` VALUES('1','101','Mehdi','Ismail','3','107','Serpina'
,'1','Ephese','3','1','0','0','2014-05-20','0615','Pacha','9','0','60','0','0',
'False','True','1','5','1',
'2014-07-02 05:14:45','1','2015-01-05 03:17:47')
当我恢复时,True 字段存储为零
我使用 StreamReader 读取 CSV 文件并使用命令 ExecutNonQuery()
该列在 mysql 5.5 中设计为 bool。
在恢复期间,此字段是否转换为字符串?
如果我使用加载数据文件而不是我的方法,这可以解决这个问题吗?
这是我恢复数据的方法(它有点长,但有些人要求看看我为此做了什么)
private void RestoreData()
{
// some declaration for labels and textboxes
DirectoryInfo d = new DirectoryInfo(pathname);
FileInfo[] Files = d.GetFiles("*.sql");
if (Files.Length == 0)
{
MessageBox.Show("There is no sql files in this folder.");
return;
}
for (int i = 0; i < Files.Length; ++i)
{
string filename = System.IO.Path.Combine(pathname, Files[i].ToString());
TotalRows = TotalRows + File.ReadAllLines(filename).Length;
MyArray[i] = File.ReadAllLines(filename).Length.ToString();
}
using (MySqlConnection conn = new MySqlConnection(PublicVariables.cs))
{
conn.Open();
for (int i = 0; i < Files.Length; ++i)
{
string filename = System.IO.Path.Combine(pathname, Files[i].ToString());
string tblname = Files[i].ToString();
tblname = tblname.Substring(0, tblname.Length - 4);
DialogResult result = MessageBox.Show("Would you restore the table " + tblname + " ?", "", MessageBoxButtons.YesNo);
MySqlCommand cmd;
if (result == DialogResult.Yes)
{
cmd = new MySqlCommand("TRUNCATE TABLE " + tblname, conn);
try
{
cmd.ExecuteNonQuery();
}
catch (MySqlException e)
{
MessageBox.Show(e.Number.ToString() + " -> " + e.Message.ToString());
conn.Close();
button5.Visible = true;
return;
}
int RowsNbr = Convert.ToInt32(MyArray[i]);
System.IO.StreamReader file = new System.IO.StreamReader(filename, Encoding.UTF8, true);
for (int a = 0; a < RowsNbr; ++a)
{
string cmdstr = file.ReadLine();
cmdstr = cmdstr.Replace("'n'r", string.Empty);
cmd = new MySqlCommand(cmdstr, conn);
try
{
cmd.ExecuteNonQuery();
}
catch (MySqlException e)
{
MessageBox.Show(e.Number.ToString() + " -> " + e.Message.ToString());
button5.Visible = true;
file.Close();
return;
}
cmd.Dispose();
++counter;
int barvalue = (counter * 100 / TotalRows);
progressBar1.Value = barvalue;
barproceed = true;
}
}
if (!barproceed)
{
counter = counter + Convert.ToInt32(MyArray[i]);
int bar1value = (counter * 100) / TotalRows;
progressBar1.Value = bar1value;
}
barproceed = false;
}
}
看起来像是在数字上下文中解释字符串值的问题。
对于 INSERT 语句,有一个字符串文字'True'
正在转换为整数。当字符串为非数字时,MySQL不会抛出异常或错误,MySQL会获取任何可以获取的前导数值,并丢弃其余的。如果MySQL找不到任何数字,则计算结果为0。 因此,许多字符串最终被评估为整数值 0。
在数字上下文中计算的字符串文本的简单演示是向它们添加 0:
SELECT d.d
, d.d + 0
FROM ( SELECT 'foo' AS d
UNION ALL SELECT 'True'
UNION ALL SELECT 'False' AS d
UNION ALL SELECT 'a1'
UNION ALL SELECT '123def'
UNION ALL SELECT ' -12-345-67'
UNION ALL SELECT '+42'
) d
d d + 0
----------- ------
foo 0
True 0
False 0
a1 0
123def 123
-12-345-67 -12
+42 42
我们看到字符串文本值'True'
和'False'
在数字上下文中的计算结果均为 0。
OP 状态:
该列在 mysql 5.5 中设计为 bool。
目前还不清楚这意味着什么,因为在MySQL 5.5中没有"BOOLEAN
"列数据类型。
MySQL确实可以识别布尔文字TRUE
和FALSE
,但这些实际上只是计算为整数值的关键字1
和0
。
布尔文字不括在单引号中。括在单引号中的值,例如 'True'
和 'False'
是字符串文字。
从"布尔值"到MySQL数据类型有几种可能的映射。
我们总是为布尔保留的数据是TINYINT(1) UNSIGNED COMMENT 'boolean'
。
我们分配文字值 1 或 0,尽管也可以使用布尔关键字 TRUE
或 FALSE
。如果我们分配一个字符串文字值 'True'
(如 MySQL 允许的那样),该值将被转换为 0
,就像'False'
转换为 0
一样。