Python论坛  - 讨论区

标题:[python-chinese] sqlite3模块insert操作后的数据为何没被保存

2006年12月03日 星期日 00:48

junyi sun ccnusjy在gmail.com
星期日 十二月 3 00:48:27 HKT 2006

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文件中?

也可能是我少了些关键的代码吧?

恳请大高手指教。。。。

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年12月03日 星期日 01:16

Yingbo Qiu qiuyingbo在gmail.com
星期日 十二月 3 01:16:27 HKT 2006

缺了 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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年12月03日 星期日 01:23

Linker Lin linkerlin88在gmail.com
星期日 十二月 3 01:23:47 HKT 2006

或者设置
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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年12月03日 星期日 12:33

junyi sun ccnusjy在gmail.com
星期日 十二月 3 12:33:05 HKT 2006

非常感谢,问题解决了。
但是为什么文档里的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
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号