2014-09-05 280 views
1

您好我是新来的Python和经历了一些教程,我的代码不给任何错误,但它失败,出现问题的完整列表是在底部SQLite的INSERT INTO失败

这是我第一次的.py文件,该文件是rehber.py

import crehber 

print("ASIM's Contact List") 

print("To create a Table press 1") 
print("To write into database press 2") 

choice = int(input("Your Choice: ")) 

def checkInput(choice): 

    if choice == 1: 
     print("Your Choice is ", choice) 
     crehber.tablecreate() 
     return True 
    elif choice == 2: 
     print("Your Choice is ", choice) 
     name = str(input("Enter name: ")) 
     surname = str(input("Enter surname: ")) 
     number = int(input("Enter number: ")) 
     crehber.tableInsert(name,surname,number) 
     return True 
    else: 
     print("Invalid choice, please re-enter") 

while checkInput(choice) != True: 
    choice = int(input("Your Choice: ")) 

,这里是另一个.py文件是crehber.py

import sqlite3 as db 

def tablecreate(): 
    conn = db.connect('rehber.db') 
    cursor = conn.cursor() 
    cursor.execute('create table bilgiler(name text,sname text, num text)') 
    print("Table is created") 

def tableInsert(name,surname,number): 
    conn = db.connect('rehber.db') 
    cursor = conn.cursor() 
    cursor.execute('insert into bilgiler(name,sname,num) values(?,?,?)', (name,surname,number)) 
    print("Insert is Successful") 
    conn.close() 

附加信息:

1)SQLite是在C:\ sqlite的 2-)我创建了一个目录c:\ pythonb一起玩,这是我的.py文件是

问题列表&问题:

1 - 在sqlite目录中,并输入sqlite3 rehber.db时,该文件在那里创建。 然后当我运行该程序,并调用

tableCreate() function 

rehber.db出现在pythonb目录,这正常吗?

2-当我做一些条目,并得到“插入成功”的消息 我去sqlite目录并输入sqlite3 rehber.db 然后选择* from bilgiler; 但是什么都没有出现..你认为什么是错的?

3-我有时会错过分号;在select命令和我卡在sqlite shell后,如何退出而不关闭commandprompt?

感谢节约您的时间在阅读,任何帮助表示赞赏

回答

1

1-当源码目录和我的sqlite3 rehber.db类型,文件创建在那里。那么当我运行该程序并调用tableCreate()函数rehber.db出现在pythonb目录中时,这是否正常?

我不知道“正常”,但是当你只是指定一个没有任何路径名的文件时 - 在任何程序中 - 你都会在当前工作目录下得到一个文件。因此,如果您打开DOS提示符cd c:\sqlite,然后运行sqlite3 rehber.db,则会创建一个文件c:\sqlite\rehber.db,因为c:\sqlite是您当前的工作目录。如果您打开DOS提示符cd c:\pythonbrehber.py,则会创建一个文件c:\pythonb\rehber.db,因为c:\pythonb是您当前的工作目录。

2-当我做一些条目并获得“Insert is successful”消息时,我进入sqlite目录并键入sqlite3 rehber.db,然后从bilgiler中选择*;但没有出现..你认为什么是错的?

那么,一个程序(您的脚本)在一个文件上运行,另一个(sqlite3)在一个完全不同的文件上运行。没有什么出现的原因相同,因为当您填满汽车的油箱时,我的汽车油箱中没有任何东西出现。

3-我有时会错过分号;在select命令和我卡在sqlite shell后,如何退出而不关闭commandprompt?

您只需在继续提示行(...>)上输入分号即可完成最后一条命令。

+0

当我在pythonb中运行rehber.db时,我没有得到任何输出..我应该将rehber.db从sqlite目录复制到pythonb目录然后运行程序? – Licentia 2014-09-06 06:49:20