2006年12月03日 星期日 00:48
write.py (用于创建表,并写入数据) ----------------------------------------------------------------------------------- import sqlite3 conn = sqlite3.connect('data.dat') c = conn.cursor() c.execute(''' create table members( id int, name varchar, login timestamp ) ''') c.execute(''' insert into members values (1,'jeff','2006-10-01') ''') c.execute(''' insert into members values (2,'angie','2006-10-02') ''') c.execute(''' insert into members values (3,'dylan','2006-10-03') ''') res = c.execute('select * from members') for row in res: print row ---------------------------------------------------------------------------- read.py (用于从表中读出数据) ------------------------------------------------------------------------------ import sqlite3 conn = sqlite3.connect('data.dat') c = conn.cursor() res = c.execute('select * from members') for row in res: print row ----------------------------------------------------------------------------- 先执行write.py,再执行read.py,效果如下: C:\>write.py (1, u'jeff', u'2006-10-01') (2, u'angie', u'2006-10-02') (3, u'dylan', u'2006-10-03') C:\>read.py 也就是说insert插入的数据只是在内存中,而没有保存到硬盘的data.dat文件中? 也可能是我少了些关键的代码吧? 恳请大高手指教。。。。
2006年12月03日 星期日 01:16
缺了 commit 2006/12/3, junyi sun <ccnusjy at gmail.com>: > write.py (用于创建表,并写入数据) > ----------------------------------------------------------------------------------- > import sqlite3 > conn = sqlite3.connect('data.dat') > c = conn.cursor() > c.execute(''' > create table members( > id int, > name varchar, > login timestamp > ) > ''') > c.execute(''' > insert into members > values (1,'jeff','2006-10-01') > ''') > c.execute(''' > insert into members > values (2,'angie','2006-10-02') > ''') > c.execute(''' > insert into members > values (3,'dylan','2006-10-03') > ''') > > res = c.execute('select * from members') > for row in res: > print row > > ---------------------------------------------------------------------------- > > read.py (用于从表中读出数据) > ------------------------------------------------------------------------------ > import sqlite3 > conn = sqlite3.connect('data.dat') > c = conn.cursor() > res = c.execute('select * from members') > for row in res: > print row > > ----------------------------------------------------------------------------- > 先执行write.py,再执行read.py,效果如下: > C:\>write.py > (1, u'jeff', u'2006-10-01') > (2, u'angie', u'2006-10-02') > (3, u'dylan', u'2006-10-03') > > C:\>read.py > > 也就是说insert插入的数据只是在内存中,而没有保存到硬盘的data.dat文件中? > > 也可能是我少了些关键的代码吧? > > 恳请大高手指教。。。。 > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese
2006年12月03日 星期日 01:23
或者设置 autocommit=true On 12/3/06, Yingbo Qiu <qiuyingbo在gmail.com> wrote: > > 缺了 commit > > 2006/12/3, junyi sun <ccnusjy在gmail.com>: > > write.py (用于创建表,并写入数据) > > > ----------------------------------------------------------------------------------- > > import sqlite3 > > conn = sqlite3.connect('data.dat') > > c = conn.cursor() > > c.execute(''' > > create table members( > > id int, > > name varchar, > > login timestamp > > ) > > ''') > > c.execute(''' > > insert into members > > values (1,'jeff','2006-10-01') > > ''') > > c.execute(''' > > insert into members > > values (2,'angie','2006-10-02') > > ''') > > c.execute(''' > > insert into members > > values (3,'dylan','2006-10-03') > > ''') > > > > res = c.execute('select * from members') > > for row in res: > > print row > > > > > ---------------------------------------------------------------------------- > > > > read.py (用于从表中读出数据) > > > ------------------------------------------------------------------------------ > > import sqlite3 > > conn = sqlite3.connect('data.dat') > > c = conn.cursor() > > res = c.execute('select * from members') > > for row in res: > > print row > > > > > ----------------------------------------------------------------------------- > > 先执行write.py,再执行read.py,效果如下: > > C:\>write.py > > (1, u'jeff', u'2006-10-01') > > (2, u'angie', u'2006-10-02') > > (3, u'dylan', u'2006-10-03') > > > > C:\>read.py > > > > 也就是说insert插入的数据只是在内存中,而没有保存到硬盘的data.dat文件中? > > > > 也可能是我少了些关键的代码吧? > > > > 恳请大高手指教。。。。 > > _______________________________________________ > > python-chinese > > Post: send python-chinese在lists.python.cn > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- Linker M Lin linkerlin88在gmail.com ※※※※※※※※※ ※※我思故我在※※ ※※※※※※※※※ -------------- 下一部分 -------------- 一个HTML附件被移除... URL: http://python.cn/pipermail/python-chinese/attachments/20061203/e96e2315/attachment.html
2006年12月03日 星期日 12:33
非常感谢,问题解决了。 但是为什么文档里的sample code都没有加上conn.commit()这句话呢? 看来还是我看得不够仔细啊 On 12/3/06, Linker Lin <linkerlin88在gmail.com> wrote: > 或者设置 > autocommit=true > > > On 12/3/06, Yingbo Qiu <qiuyingbo在gmail.com> wrote: > > 缺了 commit > > > > 2006/12/3, junyi sun <ccnusjy在gmail.com>: > > > write.py (用于创建表,并写入数据) > > > > ----------------------------------------------------------------------------------- > > > import sqlite3 > > > conn = sqlite3.connect('data.dat') > > > c = conn.cursor() > > > c.execute(''' > > > create table members( > > > id int, > > > name varchar, > > > login timestamp > > > ) > > > ''') > > > c.execute(''' > > > insert into members > > > values (1,'jeff','2006-10-01') > > > ''') > > > c.execute(''' > > > insert into members > > > values (2,'angie','2006-10-02') > > > ''') > > > c.execute(''' > > > insert into members > > > values (3,'dylan','2006-10-03') > > > ''') > > > > > > res = c.execute('select * from members') > > > for row in res: > > > print row > > > > > > > ---------------------------------------------------------------------------- > > > > > > read.py (用于从表中读出数据) > > > > ------------------------------------------------------------------------------ > > > import sqlite3 > > > conn = sqlite3.connect('data.dat') > > > c = conn.cursor() > > > res = c.execute('select * from members') > > > for row in res: > > > print row > > > > > > > ----------------------------------------------------------------------------- > > > 先执行write.py,再执行read.py,效果如下: > > > C:\>write.py > > > (1, u'jeff', u'2006-10-01') > > > (2, u'angie', u'2006-10-02') > > > (3, u'dylan', u'2006-10-03') > > > > > > C:\>read.py > > > > > > 也就是说insert插入的数据只是在内存中,而没有保存到硬盘的data.dat文件中? > > > > > > 也可能是我少了些关键的代码吧? > > > > > > 恳请大高手指教。。。。 > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese在lists.python.cn > > > Subscribe: send subscribe to > python-chinese-request在lists.python.cn > > > Unsubscribe: send unsubscribe to > python-chinese-request在lists.python.cn > > > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > > _______________________________________________ > > python-chinese > > Post: send python-chinese在lists.python.cn > > Subscribe: send subscribe to > python-chinese-request在lists.python.cn > > Unsubscribe: send unsubscribe to > python-chinese-request在lists.python.cn > > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > > > > -- > Linker M Lin > linkerlin88在gmail.com > ※※※※※※※※※ > ※※我思故我在※※ > ※※※※※※※※※ > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to > python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request在lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese >
Zeuux © 2025
京ICP备05028076号