0

I have a .txt file which contains a column of unixtime, like (1322485992.420381000), the number of digits before and after the dot are the same for all data. First I want to import this column into a Mysql table, how should I define the data type?

CREATE TABLE videoinfo (id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, unixtime char(50))

After that, in Python, I need to convert this column into datetime like: 2011-11-28 14:25:23 I use resultsto keep the data fetched from database,then try to convert it into datetime. But it cannot work, it says datetime.fromtimestamp requires a float. But if I use unixtime float(10,10) to create the column, data from txt file cannot be written into database.

results = cur.fetchall()
for result in results:
    times = datetime.fromtimestamp(result[0])
    cur.execute("ALTER TABLE youtube ADD date DATETIME NOT NULL DEFAULT after unixtime")
    for time in times:
        cur.execute(u'''insert into `date` values (%s)''', time)

Can anyone help? Many thanks!!!

-edit- for row in cur.fetchall(): print (row) times = datetime.fromtimestamp(float(row)) The print result is ('1322485970.084063000',)then, the error message is TypeError: float() argument must be a string or a number. So how can I fetch the pure string value to get rid of ('',)

-edit- use row[0] instead...problem solved...

1 Answer 1

1

Looks like a DOUBLE(11,9) although such a large number may cause problems on 32-bit systems. Consider using a VARCHAR(21) if that's an issue.

Also, MySQL simply understands FROM_UNIXTIME(1322485992.420381000).

-edit-

When it says "requires a float", why don't you just use float(result) ?

Sign up to request clarification or add additional context in comments.

6 Comments

ok, but I have a column of data needed to be converted, not just a single one
I used float to define the column, but like I said, the problem is the data in txt file cannot be written into database, but when it's defined as char, it can be written
Actually I meant using float() in Python: times = datetime.fromtimestamp(float(result[0]))
Ok, I tried, got error: ValueError: invalid literal for float(): None
In that case, as the error says, result[0] is None which explains why you can't insert it :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.