2015-04-26 70 views
0

我想寫一個函數來做一個簡單的插入。 這是我迄今Python3和Sqlite3不能插入

#! /usr/bin/env python3 
#import 
import sqlite3 as lite 

#trying an insert version 1 (does nothing) 

def createTableTask(): 
""" 
Create a new table with the name Task 
""" 
#Connnection to the database and cursor creation 
con = lite.connect('./exemple.sqlite') 
con.row_factory = lite.Row 
cur = con.cursor() 
#that does nothing 
try: 
    cur.execute('''CREATE TABLE Tasks (\ 
    Name TEXT PRIMARY KEY, \ 
    Description TEXT, \ 
    Priority TEXT);''') 
except lite.IntegrityError as error_SQLite: 
    print("error: "+ str(error_SQLite)) 
else: 
    print("No error has occured.") 
con.close(); 


def insert1(): 
    """ 
    insert a new task 
    """ 
    #Allocating variables data 
    taskName = 'finish code' 
    taskDescription = 'debug' 
    taskPriority = 'normal' 
    #Connnection to the database and cursor creation 
    con = lite.connect('./exemple.sqlite') 
    con.row_factory = lite.Row 
    cur = con.cursor() 
    #that does nothing 
    try: 
     with con: 
      cur.execute('''INSERT INTO Tasks (Name, Description, Priority) \ 
      VALUES (?, ?, ?)''', (taskName, taskDescription, taskPriority)) 
    except lite.IntegrityError as error_SQLite: 
     print("error: "+ str(error_SQLite)) 
    else: 
     print("No error has occured. but no insert happend ?") 
    con.close(); 

def showResult(): 
    """ 
    Show the result of the insert 
    """ 
    con = lite.connect('./exemple.sqlite') 
    con.row_factory = lite.Row 
    cur = con.cursor() 
    cur.execute\ 
    ('''SELECT * FROM Tasks ;''') 
    row = cur.fetchone() 
    while row: 
     print(row["Name"], ' | ', row["Description"], ' | ', \ 
     row["Priority"]) 
     row = cur.fetchone() 
    con.close(); 


#trying an insert version 2 (this one crash giving :Value error) 
def insert2(): 
    """ 
    insert a new task 
    """ 
    #Allocating variables data 
    taskName = 'finish code' 
    taskDescription = 'debug' 
    taskPriority = 'normal' 
    #Connnection to the database and cursor creation 
    con = lite.connect('./exemple.sqlite') 
    con.row_factory = lite.Row 
    cur = con.cursor() 
    queryInsert = ('''INSERT INTO Tasks (Name, Description, Priority) \ 
    VALUES (?, ?, ?)''', (taskName, taskDescription, taskPriority)) 
try: 
    with con: 
     cur.execute(queryInsert) 
except lite.IntegrityError as error_SQLite: 
    print("error: "+ str(error_SQLite)) 
else: 
    print("No error has occured.") 
con.close(); 

def run(): 
    createTableTask() 
    insert1() 
    showResult() 
    insert2() 
    showResult() 

#calling section 
run() 

的問題是,沒有任何插件的,我迄今取得的工作嘗試。 第一個沒有什麼,但有一個正確的語法
第二個,它崩潰了。

這裏是輸出:

[email protected]:~/Documents/testing$ ./exemp.py
No error has occured.
No error has occured. but no insert happend ?
Traceback (most recent call last):
File "./exemp.py", line 98, in
run()
File "./exemp.py", line 94, in run
insert2()
File "./exemp.py", line 83, in insert2
cur.execute(queryInsert)
ValueError: operation parameter must be str
[email protected]:~/Documents/testing$ sqlite3 exemple.sqlite
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> SELECT * FROM Tasks;
sqlite>

我正在尋找最簡單的解決,也許知道什麼是錯我的代碼。因爲現在我不知道沒有插入的是怎麼回事。通常它應該,還是我錯過了什麼?

回答

1
queryInsert = ('''INSERT ...''', (taskName, taskDescription, taskPriority)) 

這使得queryInsert元組有兩個元素。
但是要調用execute方法,則需要兩個單獨的參數。

你可以只把它解析:

cur.execute(*queryInsert) 

但它可能是更清晰的使用兩個獨立的變量:

queryString = '''INSERT ...''' 
queryParams = (taskName, taskDescription, taskPriority) 

cur.execute(queryString, queryParams) 
0

好,我在我的錯誤了。只是發佈它,因爲它可能會幫助其他人。 cur.execute()是一個尋找查詢的函數,因爲它是第一個參數,而另一個參數是查詢所需的變量。

步驟一:讓查詢到一個變量沒有它的參數

queryString = ''' INSERT INTO someTables rowName, rowName2 ... VALUES (?, ?);''' 

應該有儘可能多的?因爲有需要的變量。在本例,我需要2

queryValue1 = 'something' 
queryValue2 = '123' 

步驟2調用和執行查詢:

cur.execute(queryString, queryValue1, queryValue2) 

這應該沒有問題

合作