Dealing with some seriously incompetent database design here. Moving an app from MS Access to mySQL and for the moment it is important to preserve table names. However the Access db creator has spaces in his table names...
I tried doing this import with soft quotes, hard quotes, backticks, and no quotes but all give
"check the manual that corresponds to your MySQL server version for the right
syntax to use near 'Citation Table' at line 1,
when using table: Chain Citation Table"
I saw that you can escape spaces in some commands eg rm Chain\ Citation\ Table.txt but I get the same error from that.
Here is an example:
mysqlimport --host=mysql.myhost.com --user=dbuser -p \
--local --delete \
--fields-optionally-enclosed-by='|' \
--fields-terminated-by=';' \
--lines-terminated-by='\n' \
dbname "Chain Citation Table.txt"
What is the right way to handle this messed up situation? Do I have to make a holding table named SomethingWithoutSpaces and import to it and then copy across?
Thanks for any advice.
You could try using this Access to MySQL converter , it has worked well for me in the past.
In the end my solution is to split the massive delimited file into parts and import via phpMyAdmin. If anyone knows a way to specify a table name with spaces in it for mysqlimport syntax I would appreciate their help!
// 4.19.2011 Update
Found a better way using SQL, duh.
TRUNCATE TABLE `Chain Citation Table`;
LOAD DATA LOCAL INFILE ' Chain Citation Table.txt' INTO TABLE `Chain Citation Table`;
Related
I have hundreds of SQL file which I want to restore all of the databases in different database name for each file.
I look around for a solution, but what I got is something like concat all the files into one SQL file using cat.* and then restore using the concatenated file.
But, what I want is to restore it to a different database so, I think concat is not suitable for my case.
Here's one solution: alternate USE commands with your sql files, so you change the default database before the respective database's content. Gather the whole collection together and then pipe that to the input of the mysql client.
Example using bash syntax:
(
echo "USE database1;"
cat file1.sql
echo "USE database2;"
cat file2.sql
...
) | mysql
Another solution is to run the mysql client once for each file, and specify the database name as the argument:
mysql database1 < file1.sql
mysql database2 < file2.sql
...
Re your comment:
You can write a loop in bash too.
for file in *.sql
do
db=...
mysql $db < $file
done
The tricky part above is the "..." — deciding which db goes with each input SQL file. You haven't described any way to match them, so I don't know what you'd have to do to figure that out. But if you can make that inference somehow from the filename, then you can do this without having to type every file.
I am studying mysql from http://dev.mysql.com/doc/refman/5.7/en/batch-mode.html . On second paragraph it says:
If you are running mysql under Windows and have some special characters in the file that cause problems, you can do this:
C:\> mysql -e "source batch-file"
What are special characters? If I save a file in notepad, would there be automatic special characters saved in the file? How to know whether they are there or not? Are they hidden?
What would the -e flag do? Where can I find its explanation in mysql documentation?
-e is actually short for --execute, that's probably why you had trouble finding it. http://dev.mysql.com/doc/refman/5.7/en/mysql-command-options.html#option_mysql_execute
Execute the statement and quit. The default output format is like that
produced with --batch. See Section 5.2.4, “Using Options on the
Command Line”, for some examples. With this option, mysql does not use
the history file.
A special charater is something that needs to be escaped in an SQL query. You will know when you run into them because mysql will produce errors.
I have access to a MySQL database hosted on a remote server. I am attempting to migrate this to a local SQLite database. To do this, I am using this script, as suggested by this question. The usage is
./mysql2sqlite mysqldump-opts db-name | sqlite3 database.sqlite
I tried doing exactly that (with no dump options) and sqlite3 returned an error:
Error: near line 4: near "SET": syntax error
So far, I have found that when I only specify one of my tables in the dump options like so
./mysql2sqlite db-name table-B | sqlite3 database.sqlite
It appears to work fine, but when I specify the first table (let's call it table-A) it returns this error. I'm pretty sure it's returning this error because of the output of mysql2sqlite. The 4th line (I guess the 4th logical line, or the command that starts on the 4th actual line) of the dump file looks like this:
CREATE TABLE "Association_data_interaction" (
"id" int(10) DEFAULT NULL,
...
"Comments" text CHARACTER SET latin1,
...
"Experiment" text CHARACTER SET latin1,
"Methods" text CHARACTER SET latin1,
...
);
With many other rows removed. I don't really know SQL that well, but as far as I can tell, the migration script is trying to output a dump file with commands that can create a new database, but the script has to translate between MySQL's output commands and the commands sqlite3 wants to create a database, and is failing to properly handle the text fields. I know that when I run SHOW COLUMNS; in the MySQL database the Comments, Experiment, and Methods columns are of the "text" type. What can I do make sqlite3 accept the database?
Note: I have editing access to the database, but I would much prefer to avoid that if at all possible. I do not believe I have administrative access to the database. Also, if it's relevant, the database has about 1000 tables, most of which have about 10,000 rows and 10-50 columns. I'm not too interested in the performance characteristics of the database; they're currently good enough for me.
That script is buggy; one of the bugs is that it expects a space before the final comma:
gsub( /(CHARACTER SET|character set) [^ ]+ /, "" )
Replace that line with:
gsub( /(CHARACTER SET|character set) [^ ]+/, "" )
I recently migrated domains and in my database, I had stored full paths containing the old domain, which now broke :)
What I need to do is change values in the database table from
http://www.olddomain.com/img/some/path
to
http://www.newdomain/same/dir/structure/as/old/domain
The only caveat is that the photo names at the end of the url must be preserved. So essentially, I have to just change the host name.
Is that possible to do? If so, how? :)
Try this:
UPDATE table SET column = REPLACE(column,"www.olddomain.com","www.newdomain.com");
ALWAYS make sure you do a backup of your database before running a query that updates many records (as this will).
With a MySQL database, do this on the command line:
1 - Put full DB in a file:
mysqldump -uYOURUSERNAME -pYOURPASSWORD YOURDBNAME > YOURDBNAME.sql
2 - Replace olddomain.com with newdomain.com in the previous DB file:
sed -i 's/olddomain.com/newdomain.com/g' YOURDBNAME.sql
3 - Delete all tables in original database (make sure you have a backup), and update database with replaced domain in all rows of all tables, where applicable:
mysql -uYOURUSERNAME -pYOURPASSWORD YOURDBNAME < YOURDBNAME.sql
This is guaranteed to work. I've used this to update domains on Magento databases (300+ tables) several times.
FYI, sed is a linux/unix command line tool for "filtering and transforming text", I don't know if there is a windows version.
PS - If you really need to put slashes (/) in the domain (like if you're replacing www.example.com/sitedir with www.example.com), you should escape the slashes inside the sed string, i.e. instead of using /, use \/. For this example you would do:
sed -i 's/www.example.com\/sitedir/www.example.com/g' YOURDBNAME.sql
What's the easiest way to get the data for a single table, delete a single table or break up the whole dump file into files each containing individual tables? I usually end up doing a lot of vi regex munging, but I bet there are easier ways to do these things with awk/perl, etc. The first page of Google results brings back a bunch of non-working perl scripts.
When I need to pull a single table from an sql dump, I use a combination of grep, head and tail.
Eg:
grep -n "CREATE TABLE" dump.sql
This then gives you the line numbers for each one, so if your table is on line 200 and the one after is on line 269, I do:
head -n 268 dump.sql > tophalf.sql
tail -n 69 tophalf.sql > yourtable.sql
I would imagine you could extend upon those principles to knock up a script that would split the whole thing down into one file per table.
Anyone want a go doing it here?
Another bit that might help start a bash loop going:
grep -n "CREATE TABLE " dump.sql | tr ':`(' ' ' | awk '{print $1, $4}'
That gives you a nice list of line numbers and table names like:
200 FooTable
269 BarTable
Save yourself a lot of hassle and use mysqldump -T if you can.
From the documentation:
--tab=path, -T path
Produce tab-separated data files. For each dumped table, mysqldump
creates a tbl_name.sql file that contains the CREATE TABLE statement
that creates the table, and a tbl_name.txt file that contains its
data. The option value is the directory in which to write the files.
By default, the .txt data files are formatted using tab characters
between column values and a newline at the end of each line. The
format can be specified explicitly using the --fields-xxx and
--lines-terminated-by options.
Note This option should be used only when mysqldump is run on the
same machine as the mysqld server. You must have the FILE privilege,
and the server must have permission to write files in the directory
that you specify.
This shell script will grab the tables you want and pass them to splitted.sql.
It’s capable of understanding regular expressions as I’ve added a sed -r option.
Also MyDumpSplitter can split the dump into individual table dumps.
Maatkit seems quite appropriate for this with mk-parallel-dump and mk-parallel-restore.
I am a bit late on that one, but if it can help anyone, I had to split a huge SQL dump file in order to import the data to another Mysql server.
what I ended up doing was splitting the dump file using the system command.
split -l 1000 import.sql splited_file
The above will split the sql file every 1000 lines.
Hope this helps someone