I tried to use timestamp to forge a 14 digits int in form of YYYYMMDDHHMMSS in MySQL DB as primary key for later indexing. While 14 digits integer in MySQL is too large for int datatype, so I created table in bigint datatype. I have converted the timestamp into int with my python codes, but while I connected to MySQL, then trying insert 14 digits int into tables with bigint tables. It seems not working. And I queried tables with previous inserted rows for testing, it returns 20771213011177L. With a L in the end. So I noticed it might be the datatypes causing the issue. I try to convert the int into long, but the failure is still there. Is there a way I can insert 14 digits INT into MySQL DB with BIGINT datatype or I have to convert it into some other way? By the way, I'm using python 2.
The table was created this way:
CREATE TABLE IF NOT EXISTS table_1 (
table_primary_key BIGINT(14) PRIMARY KEY,
value1 DOUBLE,
value2 DOUBLE,
event_date_time DATE
);
Then here are codes:
from __future__ import print_function
from collections import OrderedDict
import serial, struct, sys, time, json, subprocess
import datetime
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "admin",
password = "S2Bgz4AnVWLQQVun",
db = "testdb"
)
mycursor = mydb.cursor()
JSON_FILE = '/home/yser/proofvalues.json'
...
if __name__ == "__main__":
currentDateTime = datetime.datetime.now()
primaryKey = int(currentDateTime.strftime("%Y%m%d%H%M%S"))
primaryKey_long = long(primaryKey)
..
# sensor reads and process out with two working variables
..
# This json is for proof values.
try:
with open(JSON_FILE) as json_data:
data = json.load(json_data)
except IOError as e:
data = []
# check if length is more than 1000 and delete first element
if len(data) > 1000:
data.pop(0)
# append new values
jsonrow = {'table_primary_key': primaryKey,'value1': values[0], 'value2': values[1], 'event_date_time': currentDateTime.strftime("%Y-%m-%d %H:%M:%S")}
data.append(jsonrow)
try:
mycursor.execute("INSERT INTO table_1(table_primary_key, value1, value2, event_date_time) VALUES (%s, %s, %s, %s)",(primaryKey, values[0], values[1], currentDateTime.strftime("%Y-%m-%d %H:%M:%S")))
mydb.commit()
except:
mydb.rollback()
sys.exit()
SELECT dt+0, ...