mysql - 从文本文件中的术语列表创建 MySQL 表

标签 mysql database linux text field

我有一个文本文件,其中包含一长串术语(大约 800 个),按格式按字母顺序排序:

aword
bword
cword
...

I would like to use this file to create a new MySQL table where each term is a field, all with the property VARCHAR(5).

Would be best if the fields were inserted in the order in which they appear in the file as I also use this file in scripts for content analysis and it would be easier if the (alphabetical) order was maintained when it came time to insert data into this new table.

For those curious, I am a student doing a thesis project in the history of science that involves content analysis. The fields will be used to hold frequency data. There will be another field that will the primary key and link these fields with other data. I am no programmer, but do have 10 years experience running linux and am usually able to figure things out. However, Google has failed me here.

EDIT

So Damoviso pointed out that what I really needed to do was turn the file into a mySQL command. I used awk and uniq to generate the following:

CREATE TABLE keyterms_frq (
    filename VARCHAR(20),
    apperception VARCHAR(5),
    behaviorism VARCHAR(5),
    behavioristic VARCHAR(5),
    behaviorists VARCHAR(5),
    behaviorist VARCHAR(5),
    behavior VARCHAR(5),
    behaviour VARCHAR(5),
    brain VARCHAR(5),
    conditioned VARCHAR(5),
    conditioning VARCHAR(5),
    condition VARCHAR(5),
    consciousness VARCHAR(5),
    conscious VARCHAR(5),
    experienced VARCHAR(5),
    experiences VARCHAR(5),
    experience VARCHAR(5),
    intellect VARCHAR(5),
    introspections VARCHAR(5),
    introspection VARCHAR(5),
    introspectively VARCHAR(5),
    introspective VARCHAR(5),
    intuition VARCHAR(5),
    memoryimage VARCHAR(5),
    memory VARCHAR(5),
    mentality VARCHAR(5),
    mentally VARCHAR(5),
    mental VARCHAR(5),
    mind VARCHAR(5),
    mirrorscript VARCHAR(5),
    mirrorwriting VARCHAR(5),
    unconditioned VARCHAR(5)
);  

不幸的是,这仍然无法正常工作,因为它会生成以下内容:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition VARCHAR(5), consciousness VARCHAR(5), conscious VARCHAR(5), experience' at line 1

我最初认为我可以生成多少个字段是有限制的,但这似乎不是问题

最佳答案

就我个人而言(我知道这可能不会流行),但我倾向于使用 Excel 和诸如 Textpad 或 Notepad++(甚至是普通记事本)之类的东西来进行一次性数据库插入和表格创建,就像这样。

导入文本文件,使每个单词都在一个新行中,根据需要对它们进行排序,并通过在顶部和底部插入列和行来围绕它构建 SQL 命令。

在这种情况下,我建议:

  1. 在单元格 A1 的顶部插入一行,内容如下:
    CREATE TABLE MyTable(Id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  2. 在 B 列中单词旁边的每个单元格中,输入以下内容:
    VARCHAR(5),
  3. 删除最后一个逗号并在下面的单元格中放置一个“”;
  4. 另存为制表符分隔的文件,然后使用您选择的文本编辑器,将制表符替换为空格。

您将得到一个可以针对数据库运行的 SQL 文件。

是的,它很乱而且不是很 1337,但它很有效。

关于mysql - 从文本文件中的术语列表创建 MySQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/641438/

相关文章:

php - 将未知数量的字符串转换为变量

php - 如何使用 while 循环显示 mysql_fetch_array 的所有行

php - 无法根据时间戳从一组消息中获取最后一条消息,其中名称作为查询字符串传递

linux - Linux selenium自动化测试问题: firefox can't be closed automatically

c - Linux堆分配

java - JPA 唯一重命名唯一索引

mysql - 更改 php.ini 后仍然收到带有 XAMPP 错误的 mutillidae

mysql - 更新多行,增加值(value)。(不是autoinc。)

java - 在下拉列表中显示从数据库中选择的数据

linux - .htaccess 是否会干扰嵌套的 .htaccess/.htpasswd?