A K
A K

Reputation: 183

Unable to insert any data to the database using python

import sqlite3

def create_a_new_table():
    Myschool = sqlite3.connect('normal database for experiment purpose.db')
    curschool = Myschool.cursor()
    curschool.execute("""
                           CREATE TABLE new_Basic_Player_Info
                           (
                              Ranking INTEGER,
                              Player_name TEXT,

                              Country TEXT,
                              Speciality TEXT,
                              Value INTEGER,
                              Cost INTEGER
                            );
                      """)
    Myschool.close()


def insert_data():
    Myschool = sqlite3.connect('normal database for experiment for purpose.db')
    curschool = Myschool.cursor()
   # nm = input("Enter the name of the player: ")
    sql = """INSERT INTO TABLE new_Basic_Player_Info(Ranking, Player_name) 
        VALUES(%s, %s);"""

May be %s, %s is the problem. Or, sql named string should be ended with a semi-colon(;) The main code that is not being executed

try:
    curschool.execute(sql, (1, "aNIKET GHOSH"))
    Myschool.commit()
    Myschool.close()

except:
    Myschool.rollback()

even I have used try and except keywords too.

create_a_new_table()
insert_data()

Upvotes: 0

Views: 44

Answers (1)

Mattias
Mattias

Reputation: 160

I tested your code. I found three issues.

One issue which is that you are using two different sqlite files (missing a "for" in the first one). A good trick is to always think about DRY (Don't Repeat Yourself).

Two issues with your insert statement:

  1. INSERT INTO TABLE ... whereas the correct syntax is INSERT INTO ...

  2. You are using %s instead of the correct syntax ?

I.e. replace

sql = """INSERT INTO TABLE new_Basic_Player_Info(Ranking, Player_name) VALUES(%s, %s);"""

with

sql = """INSERT INTO new_Basic_Player_Info(Ranking, Player_name) VALUES(?, ?);"""

and you will be on track again.

Upvotes: 1

Related Questions