2

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() 
4
  • Post your code instead of describing it. You describe a lot of serious bugs though. There's absolutely no reason to store dates and times as strings or string-like integers. All databases have date-related types. Both dates and numbers are binary types, not stings with suffixes. Use the proper type and pass the values you want as parameters, not stings Commented Jul 8, 2021 at 7:26
  • BTW Python 2 is past its end-of-life date. Not just out of support, abandoned. It was replaced by Python 3 years ago. Which library do you use to connect to MySQL? How you execute queries, and how you pass types, depends on the library. MySQL.Connector/Python works one way, sqlalchemy in a different way. Commented Jul 8, 2021 at 7:39
  • Why you do not store timestamp in some date-time datatype (DATETIME, TIMESTAMP)? If you need to retrieve stored value as shown long integer then you can simply do SELECT dt+0, ... Commented Jul 8, 2021 at 7:44
  • Why you do not store timestamp in some date-time datatype (DATETIME, TIMESTAMP)? If you need to retrieve stored value as shown long integer then you can simply do SELECT dt+0, ... – Akina Because I need to use it as primary key. I'm not sure if TIMESTAMP() store in date datatype can serve as primary key or later be used as foreign key. Commented Jul 8, 2021 at 11:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.