2017-06-20 21 views
0

我嘗試了各種方法,但我不斷收到一個返回的對象。使用COUNT在sqlite3中查找表中的行數

import sqlite3 

connection = sqlite3.connect('films.db') 
cur = connection.cursor() 

def createTable(): 

    connection.execute('CREATE TABLE IF NOT EXISTS FILMS(TITLE TEXT NOT NULL , YEAR INT NOT NULL,RATING INT NOT NULL,unique(TITLE))') 
    connection.execute('INSERT OR IGNORE INTO FILMS VALUES(? , ? , ?)',('Middle Men','2010','6.6')) 
    connection.execute("SELECT * FROM FILMS") 

    row_count = cur.execute("SELECT Count(*) FROM FILMS") 
    print(row_count) 

    cur.close() 
    connection.commit() 
createTable() 

我不斷收到這個遊標對象返回當我打印計數:在0x027CE820 sqlite3.Cursor對象是有什麼原因呢?我的語法錯了嗎?

+0

你必須計算的東西爲一列,例:https://www.techonthenet.com/sqlite/functions/count.php –

回答

0

您正在提取整個對象。相反的:

row_count = cur.execute("SELECT Count(*) FROM FILMS") 

務必:

cur.execute("SELECT Count(*) FROM FILMS") 
row_count = cur.fetchone() 
print(row_count) 

這將讓具有計數在它的行。如果你正在做一個選擇了多行,你可以做

cur.fetchall() 
+0

或者一個襯墊:cur.execute(「從FILMS選擇計數(*)」)。fetchone() – mkingsbu

+1

非常感謝,我真的很感激它:) – Noob

相關問題