Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6756657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:33:04+00:00 2026-05-26T13:33:04+00:00

I dont see why it’s not working. I have created several databases and tables

  • 0

I dont see why it’s not working. I have created several databases and tables and obviously no problem. But I am stuck with this table which is created from django data model. To clarify what I have done, created new database and table from mysql console and try to insert from python and working. But, this one is strange for me.

class Experiment(models.Model):
    user = models.CharField(max_length=25)
    filetype = models.CharField(max_length=10)
    createddate= models.DateField()
    uploaddate = models.DateField()
    time = models.CharField(max_length=20)
    size = models.CharField(max_length=20)
    located= models.CharField(max_length=50)

Here is view in mysql console

mysql> describe pmass_experiment;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| id          | int(11)     | NO   | PRI | NULL    | auto_increment |
| user        | varchar(25) | NO   |     | NULL    |                |
| filetype    | varchar(10) | NO   |     | NULL    |                |
| createddate | date        | NO   |     | NULL    |                |
| uploaddate  | date        | NO   |     | NULL    |                |
| time        | varchar(20) | NO   |     | NULL    |                |
| size        | varchar(20) | NO   |     | NULL    |                |
| located     | varchar(50) | NO   |     | NULL    |                |
+-------------+-------------+------+-----+---------+----------------+
8 rows in set (0.01 sec)

Above pmass_experiment table is created by django ORM after python manage.py syncdb

Now I am trying to insert data into pmass_experiment through python MySQLdb

import MySQLdb
import datetime,time
import sys

conn = MySQLdb.connect(
    host="localhost",
    user="root",
    passwd="root",
    db="experiment")

cursor = conn.cursor()
user='tchand'
ftype='mzml'
size='10MB'
located='c:\'
date= datetime.date.today()
time = str(datetime.datetime.now())[10:19]

#Insert into database
sql = """INSERT INTO pmass_experiment (user,filetype,createddate,uploaddate,time,size,located)
    VALUES (user, ftype, date, date, time, size, located)"""
try:
   # Execute the SQL command
   cursor.execute(sql)
   # Commit your changes in the database
   conn.commit()
except:
   # Rollback in case there is any error
   conn.rollback()
# disconnect from server
conn.close()

But, unfortunately nothing is inserting. I am guessing it’s may be due to primary_key (id) in table which is not incrementing automatically.

mysql> select * from pmass_experiment;
Empty set (0.00 sec)

can you simply point out my mistake?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:33:04+00:00Added an answer on May 26, 2026 at 1:33 pm
    sql = """INSERT INTO pmass_experiment (user,filetype,createddate,uploaddate,time,size,located)
        VALUES (user, ftype, date, date, time, size, located)"""
    

    Parametrize your sql and pass in the values as the second argument to cursor.execute:

    sql = """INSERT INTO pmass_experiment (user,filetype,createddate,uploaddate,time,size,located)
             VALUES (%s, %s, %s, %s, %s, %s, %s)"""
    try:
       # Execute the SQL command
       cursor.execute(sql,(user, ftype, date, date, time, size, located))
       # Commit your changes in the database
       conn.commit()
    except Exception as err:
       # logger.error(err) 
       # Rollback in case there is any error
       conn.rollback()
    

    It is a good habit to always parametrize your sql since this will help prevent sql injection.

    The original sql

    INSERT INTO pmass_experiment (user,filetype,createddate,uploaddate,time,size,located)
        VALUES (user, ftype, date, date, time, size, located)
    

    seems to be valid. An experiment in the mysql shell shows it inserts a row of NULL values:

    mysql> insert into foo (first,last,value) values (first,last,value);
    Query OK, 1 row affected (0.00 sec)
    mysql> select * from foo order by id desc;
    +-----+-------+------+-------+
    | id  | first | last | value |
    +-----+-------+------+-------+
    | 802 | NULL  | NULL |  NULL | 
    +-----+-------+------+-------+
    1 row in set (0.00 sec)
    

    So I’m not sure why your are not seeing any rows committed to the database table.

    Nevertheless, the original sql is probably not doing what you intend.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have deployed a new build on Production server, but I dont see the
Some people want to see the DB structure and dont have VS, is there
I don't see how this is possible, but I really, really hate to run
There is another post here about Atan but I dont see any relevant answers:
I have looked on a lot of the questions here and I dont see
Hi i have webserver on windows xp iis 5 sp3. I see logs but
Sorry for this not being a real question, but Sometime back i remember seeing
I created a jtable in gui form in intellij and I dont see any
I see how to remove excess spaces, but dont see a simple way (other
Ive looked through the API and dont see it, but I thought Id ask

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.