V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
ikaros
V2EX  ›  MySQL

遍历多个表名的问题

  •  
  •   ikaros · 2015-04-21 16:46:55 +08:00 · 2423 次点击
    这是一个创建于 3293 天前的主题,其中的信息可能已经有所发展或是发生改变。

    cursor.execute("""select column_name, data_type, data_length, nullable from user_tab_columns where table_name=('%s')""", tab_name)

    一直提示ORA-01036错误,数据库是Oracle,原因好像是%s旁边的两个引号? 这种情况要怎样把表名传进去呢?

    7 条回复    2015-04-21 17:23:14 +08:00
    ikaros
        1
    ikaros  
    OP
       2015-04-21 16:54:44 +08:00
    转义了 ' 也不行
    MasterYoda
        2
    MasterYoda  
       2015-04-21 16:59:30 +08:00   ❤️ 1
    % tab_name
    fxxkgw
        3
    fxxkgw  
       2015-04-21 17:05:13 +08:00   ❤️ 1
    name=(%s) 而且也不需要加这么多""" """ ,直接用" "就可以了吧
    是用MySQLdb么?
    ikaros
        4
    ikaros  
    OP
       2015-04-21 17:13:28 +08:00
    @MasterYoda 这个我试过,invalid syntax = =


    @fxxkgw Oracle,%s两边那两个单引号好像是必需的,直接用一个具体的表名是可以查到表结构的 ...
    fxxkgw
        5
    fxxkgw  
       2015-04-21 17:20:26 +08:00
    @ikaros oracle没用过,只用过mysql,你tab_name是元组么?
    ikaros
        6
    ikaros  
    OP
       2015-04-21 17:20:49 +08:00
    @fxxkgw
    @MasterYoda 我错了,Oraclel里面的占位符不是%s Oracle里面是:1 :2这样的
    ikaros
        7
    ikaros  
    OP
       2015-04-21 17:23:14 +08:00
    @fxxkgw
    嗯 改成这样就好了
    cursor.execute("""select column_name, data_type, data_length, nullable from user_tab_columns where table_name=:1""", (tab_names[index],))
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2732 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 05:15 · PVG 13:15 · LAX 22:15 · JFK 01:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.