简体   繁体   中英

How To Iterate UTF-8 String To Mysql

Could please somebody help me find out how to iterate these raw txt data to mysql. The format is

user id | item id | rating | timestamp

and i want to insert these data to my table in MySql (using PHPmyAdmin), let's say the table structure is : user_id (int), item_id(int), rating(int), timestamp(int) with its name "Rating".

So, i want to know how to insert these data to my table, i'm fine with php, or if there are easier way to do this.

txt数据

If you want to generate raw SQL queries, you can do so by using find and replace in your text editor (that looks like Notepad++). I'm guessing that your delimiters are tabs.

  1. Find and replace all tab characters and replace them with a comma. We do not need to quote anything as all of your fields are integers.
  2. Find and replace all newline characters and replace them with a SQL query.

Execute these commands in regular expression mode:

Columns

Find: \\t

Replace: ,

Rows

Find: \\r\\n (if that doesn't find anything, look for \\n )

Replace: );\\r\\nINSERT INTO Rating (user_id, item_id, rating, timestamp) VALUES (

On the first row, insert the text INSERT INTO Rating (user_id, item_id, rating, timestamp) VALUES ( to make the row a valid SQL statement.

On the last row, remove any trailing portions of SQL query after the last semicolon.

Copy and paste this into your PHPMyAdmin and it should be all good.

The simplest way I have found for doing similar is to use Excel. Import the text file into a new document - judging by the look it should be easy to seperate the columns as they appear to be tab delimited. Once you have the required columns set up a string concatenation to include the values... kind of like

=CONCATENATE("INSERT INTO Rating SET user_id ='",A1,"', item_id ='",B1,"', rating ='",C1,"', timestamp ='",D1,"';")

Then repeat for all rows, copy and paste into sql client

您可以将toad用于mysql ,导入wisard,然后在导入所有数据并导出新表的sql插入后,创建具有与文件相同结构(用户ID |项目ID | Rating |时间戳)的表。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM