user2202098
user2202098

Reputation: 860

python write to SQL file

Hello i am using python 3.4

I am trying to read a SQL-file (test2.sql) and write each line to another file (test1.sql), replacing all occurrences of a string.

The code i have below works fine when writing to .txt-files, but when trying to write to .sql-files strange Chinese characters trail each line

import fileinput
import sys

g = open('test1.sql', mode='w')
with fileinput.input(files=("test2.sql")) as f:
    for line in f:
        g.write(line.replace("database1","database2"))

Upvotes: 0

Views: 4739

Answers (1)

msw
msw

Reputation: 43497

You cannot meaningfully manipulate strings in a database as if it were a plain-text file.

You need to use the database native access method to ensure that the meta-data in the database is consistent. If you do otherwise, you are nearly guaranteed to corrupt database structures.

I'm a bit surprised this worked at all. It is likely that the next database modification (create, remove, update, delete) would hopelessly corrupt the database.

Upvotes: 1

Related Questions