2
import sqlite3 as sql
import os 

db_folder = "D:/Python/databases/accounts.sqlite"
db = sql.connect(db_folder)
im = sql.Cursor()

im.execute("""CREATE TABLE IF NOT EXISTS 'accounts' (              
          id    TEXT  DEFAULT 'account' UNIQUE             
          pw    TEXT  DEFAULT 'password
          email TEXT UNIQUE 
)
""")

datas = [
  ("eren.arc1","1236"),
  ("ern.arc"),
  (none,none,"[email protected]")
]

if not os.path.exists(db_folder):
    for data in datas : 
        im.execute("""INSERT INTO 'accounts' VALUES (?,?,?)""",datas)
    db.commit()    

I ran this code and I got this error:

File "databases/db4.py", line 6, in <module>
im = sql.Cursor()
TypeError: function takes exactly 1 argument (0 given)

What is the problem? I did not see argument frequently in cursor function. Note: I used cursor() but then I took sqlite3 hasn't got cursor attribute error. Then I changed it to Cursor().

6
  • You have to pass you db connection into the cursor. The cursor does not know where to connect. Try `sql.Cursor(db)' I may be wrong I have not tested this Commented Jul 3, 2017 at 21:24
  • 1
    cursor() without capital C ? Commented Jul 3, 2017 at 21:25
  • I dont know. Test it Commented Jul 3, 2017 at 21:26
  • I looked at the docs it is lower case Commented Jul 3, 2017 at 21:29
  • @Joe I dont know exactly why but it runned Thanks Commented Jul 3, 2017 at 21:30

1 Answer 1

2

Do:

import sqlite3 as sql
import os 

sqlite_file = "db.sqlite"
conn = sql.connect(sqlite_file)
c = conn.cursor()

c.execute("""CREATE TABLE IF NOT EXISTS accounts (
          id TEXT DEFAULT 'account' UNIQUE,
          pw TEXT DEFAULT 'password',
          email TEXT UNIQUE);""")

items = [
  ("id1", "pw1", "[email protected]"),
  ("id2", "pw2", "[email protected]"),
  ("id3", "pw3", "[email protected]")
]

for item in items: 
    c.execute("""INSERT INTO accounts VALUES (?,?,?);""", item)

c.execute("""INSERT INTO accounts(id, email) VALUES ('id4', '[email protected]');""")
c.execute("""INSERT INTO accounts(email) VALUES ('[email protected]');""")

for row in c.execute('SELECT * FROM accounts'):
    print(row)

conn.commit()
conn.close()

Output:

('id1', 'pw1', '[email protected]')
('id2', 'pw2', '[email protected]')
('id3', 'pw3', '[email protected]')
('id4', 'password', '[email protected]')
('account', 'password', '[email protected]')

If you respect the order of each field, you can check the length of the item tuple to do insertions in the loop:

for item in items: 
    if len(item) == 3:
        c.execute("""INSERT INTO accounts VALUES (?,?,?);""", item)
    elif len(item) == 2:
        c.execute("""INSERT INTO accounts(pw, email) VALUES (?,?);""", item)
    elif len(item) == 1:
        c.execute("""INSERT INTO accounts(email) VALUES (?);""", item)

But (none, none, 'email') doesn't make sense here, to insert NULL data use the keyword NULL (if python it is None not none also).

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

Comments

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.