免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6741 | 回复: 10
打印 上一主题 下一主题

每周一题。(读写数据库) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-08-29 12:14 |只看该作者 |倒序浏览
第二周的题目:

1、数据库不限。
2、可以直接使用第三方模块。
3、能读能写。
4、能被import。
5、有自测试代码。

论坛徽章:
0
2 [报告]
发表于 2005-08-30 10:42 |只看该作者

每周一题。(读写数据库)

原帖由 "xichen" 发表:
第二周的题目:

1、数据库不限。
2、可以直接使用第三方模块。
3、能读能写。
4、能被import。
5、有自测试代码。



老大能不能出两个题 高手做难一些 范围广一些的题目 初学者做简单具体一点的题目 谢谢为初学者着想!

论坛徽章:
0
3 [报告]
发表于 2005-08-30 11:17 |只看该作者

每周一题。(读写数据库)

做题其实就是为初手用的。如果真是高手的话,自已就去找项目做去了,比做题可有意思多了。做题的一个目的是你的程序可以有人给点评,如果你不喜欢,自然可以不做,别人也无法给你一些建议。

论坛徽章:
0
4 [报告]
发表于 2005-08-30 13:53 |只看该作者

每周一题。(读写数据库)

其实这个题也不是很难,就是要求去找类库(比如mysql-python的),然后使用一下就行了。

不过honbj要是有有意思的题目,也可以大家share一下啊

论坛徽章:
0
5 [报告]
发表于 2005-08-30 15:34 |只看该作者

每周一题。(读写数据库)

原帖由 "xichen" 发表:
第二周的题目:

1、数据库不限。
2、可以直接使用第三方模块。
3、能读能写。
4、能被import。
5、有自测试代码。



我来第一个

  1. #!/usr/bin/python

  2. import MSSQL

  3. #读数据库
  4. db = MSSQL.connect('SQL Server IP', 'username', 'password', 'db_name')
  5. c = db.cursor()
  6. sql = 'select top 20 rtrim(ip), rtrim(dns) from detail'
  7. c.execute(sql)
  8. for f in c.fetchall():
  9.    print "ip is %s, dns is %s" % (f[0], f[1])

  10. #写数据库
  11. sqlinsert = 'insert into detail values('192.168.0.1', 'www.dns.com.cn')
  12. c.execute(sqlinsert)
复制代码

论坛徽章:
0
6 [报告]
发表于 2005-08-30 17:06 |只看该作者

每周一题。(读写数据库)

honbj 写的都没自测试代码的。
也不能被import

论坛徽章:
0
7 [报告]
发表于 2005-09-01 01:07 |只看该作者

每周一题。(读写数据库)

刚刚开始学python,照着 dircache.py写的一个数据库版本
sqlite的数据库,需要sqlobject

  1. #!/usr/bin/python

  2. from sqlobject import *
  3. from sqlobject.sqlite import *
  4. import os
  5. import os.path
  6. import tempfile

  7. temp_cache = os.path.join( tempfile.gettempdir(), 'cache.db' )

  8. SQLiteConnection = builder()
  9. conn = SQLiteConnection( temp_cache )

  10. class Db_cache( SQLObject ):
  11.      
  12.     _connection = conn  
  13.     _cacheValue = True
  14.     _lazyUpdate = True

  15.     cacheinfo = col.StringCol( default="[]" )
  16.     cachepath = col.StringCol( alternateID=True )
  17.     def _set_cachepath( self, value ):
  18.         if type( value ) == str:
  19.             self._SO_set_cachepath( value )
  20.         else:
  21.             raise 'value error'
  22.             
  23.     def _set_cacheinfo( self, value ):
  24.         if type( value ) == list:
  25.             self._SO_set_cacheinfo( str( value ) )
  26.         else:
  27.             raise 'value Error'


  28. __all__ = ["listdir", "opendir", "reset"]

  29. def reset ():
  30.     Db_cache.clearTable()
  31.       
  32. def listdir ( path ):
  33.     """List directory contents, using cache."""
  34.     Db_cache.createTable( ifNotExists=True )
  35.     try:
  36.         res = Db_cache.select( Db_cache.q.cachepath==path )
  37.         if res.count() == 1:
  38.             for xxx in res:
  39.                 cache_mtime, directory_list = eval( xxx.cacheinfo )
  40.         else:
  41.             #Not in cache
  42.             raise KeyError
  43.     except KeyError:
  44.         cache_mtime, directory_list = -1, []
  45.     try:
  46.         mtime = os.stat( path ).st_mtime
  47.     except os.error:
  48.         return []
  49.     if mtime != cache_mtime:
  50.         print cache_mtime
  51.         try:
  52.             directory_list = os.listdir( path )
  53.         except os.error:
  54.             return []
  55.         directory_list.sort()
  56.     else:
  57.         return directory_list
  58.     try:
  59.         res2 = Db_cache.get( xxx.id )
  60.         res2.cacheinfo = [mtime, directory_list]
  61.         res2.sync()
  62.     except UnboundLocalError:
  63.         Db_cache( cachepath=path, cacheinfo=[mtime, directory_list] )
  64.     return directory_list

  65. opendir = listdir

  66. def test ():
  67.     Db_cache.createTable( ifNotExists=True )
  68.     print listdir( 'c:/windows' )
  69.    
  70. if __name__ == '__main__':
  71.     ## Run test
  72.     test()
复制代码

论坛徽章:
0
8 [报告]
发表于 2005-09-01 09:50 |只看该作者

每周一题。(读写数据库)

我想用python(2.4.1)连postgresql(8.0.3)
我在python中导入pgdb
>;>;>; import pgdb
显示错误
Traceback (most recent call last):
  File "<pyshell#5>;", line 1, in -toplevel-
    import pgdb
  File "D:\Python24\lib\pgdb.py", line 330, in -toplevel-
    _connect_ = connect
NameError: name 'connect' is not defined
当我在pgdb.py中注释_connect_ = connect后,可以导入pgdb.py
但是连接数据库时显示
Traceback (most recent call last):
  File "<pyshell#7>;", line 1, in -toplevel-
    db=pgdb.connect(dsn='localhostracle',user='postgres',password='postgres',host='localhost',database='template1')
  File "D:\Python24\lib\pgdb.py", line 375, in connect
    cnx = _connect_(dbbase, dbhost, dbport, dbopt,
NameError: global name '_connect_' is not defined
未定义全局变量_connect_,应当如何处理这个问题

论坛徽章:
0
9 [报告]
发表于 2005-09-01 22:12 |只看该作者

每周一题。(读写数据库)

出现这个错误,还是建议你详细看这个模块的文档吧.

论坛徽章:
4
CU大牛徽章
日期:2013-03-13 15:29:07CU大牛徽章
日期:2013-03-13 15:29:49CU大牛徽章
日期:2013-03-13 15:30:192015亚冠之广州恒大
日期:2015-07-22 17:20:15
10 [报告]
发表于 2005-09-03 11:27 |只看该作者

每周一题。(读写数据库)

在群里我就跟你说过了 估计_connect_ = connect 应该是
_connect_=pgde.connect
当然我发现我没有这个模块 所以不好说
老梅说得对 你去看看模块的文档 应该有详细地介绍的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP