• Scrapy使用cx_Oracle插入数据时中文乱码问题

    使用Python的爬虫框架Scrapy时  往往需要将爬到的数据保存到数据库

    而这边用的是Oracle,环境配置之类的网上很多这边就不赘述了

    编码中涉及中文时 python文件开头添加 ##coding=utf-8 是必不可少的

    乱码解决:

    Linux 环境下,设置环境变量

    NLS_LANG=AMERICAN_AMERICA.AL32UTF8

     

    同理Windows环境下 乱码也跟这个没有设置的环境变量有关,但是并不是这样解决的

    Windows主要是通过os 模块修改环境变量,解决参考代码如下

    ##coding=utf-8
    # Define your item pipelines here
    #
    # Don't forget to add your pipeline to the ITEM_PIPELINES setting
    # See: http://doc.scrapy.org/topics/item-pipeline.html
    import cx_Oracle
    import os
    os.environ['NLS_LANG']="AMERICAN_AMERICA.AL32UTF8"

     

    当然网络上也有 os.environ['NLSLANG'] = 'SIMPLIFIED CHINESECHINA.UTF8' 的配置方案,如有同样问题,就测测看吧:)

  • 单icon文件关联多个icon

    ICO 关联生成工具

    实现 图片转换成icon

    再将多个icon文件植入到一个ico文件中,常用于windows软件图标

     

    使用PhotoShop将图片保存为 .ico 格式:

    将ICOFormat 插件放置到一下目录

    Adobe\Adobe Photoshop CS4\Plug-ins\Extensions

    PS菜单中存储为,选择保存格式为 .ico 即可

     

    cmd 命令行下使用

    > icobundl.exe -o tofilename.ico path/to/file1.ico path/to/file2.ico path/to/file3.ico

     

     

    工具附件下载:

    icobundl.zip

  • win8下Apache+Mysql+PHP多版本切换环境

    环境 win8 x64  apache+mysql+php(多版本)
    mysql 
    由于5.6版本下载多次安装配置后启动仍然出现异常,只好搜索使用5.5.30版本
    下载地址
    http://downloads.mysql.com/archives/mysql-5.5/mysql-5.5.30-winx64.msi
    apache
    下载地址:
    http://archive.apache.org/dist/httpd/binaries/win32/
    对应安装包
    httpd-2.2.22-win32-x86-openssl-0.9.8t.msi

    php
    下载地址:
    http://windows.php.net/download/
    对应版本 (均为 x86 Thread Safe 版本)
    5.2.17
    5.3.25
    5.4.15
    如需更早期的版本请移步至:http://museum.php.net/  下载

    安装配置过程简记:
    (注意:安装过程中:请选择自定义配置,然后设置安装目录,最好不要放在C盘,不然你会很蛋疼~~)
    1.安装mysql,按安装包提示,设置:开发者环境,root密码。按逻辑走,基本就没问题,除非你RP爆发了....
    2.安装apache,同上安装完成后,通常都能测试访问成功,除非你RP再一次爆发...
    3.由于需要多个版本的PHP,主要作测试用。解压不同版本的PHP到对应目录。我这边设定的是 d:\php\php-version 即 d:\php\php5217 等等 
    然后在对应php-version 目录下创建php.ini文件。配置参考这边就不用赘述了,如果你不知道,建议你找个地方撞撞,当然也欢迎在这边留言

    4.以上工作完成后,修改apache配置文件,
    .........
    ##php5217
    LoadModule php5_module D:/php/php5217/php5apache2_2.dll
    PHPIniDir "D:/php/php5217" 

    ##php5325
    #LoadModule php5_module D:/php/php5325/php5apache2_2.dll
    #PHPIniDir "D:/php/php5325"

    ##php5415
    #LoadModule php5_module D:/php/php5415/php5apache2_2.dll
    #PHPIniDir "D:/php/php5415"
    ........
    <IfModule dir_module>
        DirectoryIndex  index.php index.html
    </IfModule>
    .......
    <IfModule mime_module>
    .......
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php .html
    .......
    设置完成后保存,然后重启apache服务,然后写个php文件 访问查看下phpinfo()信息。

    5.配置php.ini,加载php_mysql相关扩展
    由于php存在多个版本,这边需要指定一下具体的扩展及包含目录
    以php5.2.17版本配置为例:
    extension_dir = "D:\php\php5217\ext"
    ;移除类似以下扩展语句前的分号
    extension=php_mysql.dll
    extension=php_mysqli.dll
    ;时区设置
    ;[Date] 
    date.timezone = Asia/Shanghai
    注:这边鄙人的RP也爆发了,5.2版php扩展始终加载异常,各种配置,各种重启,各种蛋疼
    幸运的是最终还是解决了,于是顺便啰嗦一下过程
    下载了一个installer.msi文件,安装后将安装目录中的 ext/* 扩展dll文件拷贝到 d:\php\php5217 扩展目录下,覆盖了一下,重启apache后,扩展加载就成功了
    msi安装包下载地址如下
    http://124.254.47.50/download/30786342/38666427/3/msi/197/11/1350667563205_11/php-5.2.17-win32-VC6-x86-installer.msi
    当然,上面并不是真正蛋疼的,郁闷的是现在切换到5.3版本,Apache无法正常启动了,shit~
    无赖,卸载5.2版安装文件。。。,那5.2版下面的扩展加载要如何解决呢
    网上大肆搜罗一番:一种方法是说把php目录下的几个dll文件拷贝到system32下(这个似乎不太好)
    还有一种方法就是把php目录下的dll动态链接库拷贝到Apache的bin目录下(原因是PHP在这边是解压版,Windows无法正常加载动态连接库文件,fk) 不过最终还是选择了这种方法,算是比较折中吧。

    6.完成以上工作后,apache-php-mysql的运行环境就基本搭建完毕了,
    根据个人需求添加相关扩展即可,如需更换php版本,修改httpd.conf文件中调用的php5模块,然后重启即可。
    虽说有点小繁琐,但毕竟不需要经常切换php版本,所以也还是勉强可以接受的
    :)

    如果你有更好的方法,希望不吝指教 :D
  • Yii webapp 常用配置

    每个人在开发的过程中需求可能不尽相同,具体需求配置 可以参考框架里面的代码注释

    废话就不多说了

    1、 Gii模块的配置

    // uncomment the following to enable the Gii tool
        'gii'=>array(
            'class'=>'system.gii.GiiModule',
            'password'=>false,//'Enter Your Password Here', //关闭密码验证
            // If removed, Gii defaults to localhost only. Edit carefully to taste.
            'ipFilters'=>false,//array('127.0.0.1','::1'), //关闭ip过滤
        ),

     

    2、数据库配置(多库配置属性参考)
    //详细db配置参数 见CDbConnection 类
    $config['components']['dbmysql'] = array(
        'class'=>'CDbConnection',
        'connectionString' => 'mysql:host=localhost;dbname=testdrive',
        'emulatePrepare' => true,
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
    );
    $config['components']['dboracle'] = array(
        'class'=>'CDbConnection',
        'connectionString' => 'oci:dbname=//localhost:1521/testdb;charset=AL32UTF8',
        'emulatePrepare' => true,
        'username' => 'scott',
        'password' => 'tiger',
        'tablePrefix' => 'TB_',
        //'schemaCachingDuration' => 3600,
        //'schemaCachingExclude' => array(),
    );

     

    3、url 配置

    $config['components']['urlManager'] = array(
        'urlFormat'=>'path',
        'urlSuffix' => '.html', //后缀 伪静态
        'rules'=>array(
            '<controller:\w+>/<id:\d+>'=>'<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
        ),
    );

     

    4、日志及调试信息配置

    $config['components']['log'] = array(
        'class'=>'CLogRouter',
        'routes'=>array(
            array(
                'class'=>'CFileLogRoute',
                'levels'=>'error, warning',
            ),
            // uncomment the following to show log messages on web pages
           
            array(
                'class'=>'CWebLogRoute',
                'categories'=>'system.db.CDbCommand', //只查看与数据库交互的相关信息
                //'levels'=>'',
            ),
           
        ),
    );

     

    待补充

  • Yii 多表关联取数据 relations 写法

    常用relations 写法参考:
    如: A为用户表, B为用户标签表, C为标签信息表
    数据表存储 结构
    table A    ID    NAME
    table B    ID    A_ID  TYPE_ID      
    table C    ID    VALUE
    其中A表中ID 与 B表中A_ID 关联 ( 1对n)
       B表中的TYPE_ID 与 C表中的ID 关联  (1对1)
    需求示例: 查询并显示用户名为 ***  的用户的 所有标签详细信息
    简要分析:查询条件 A.NAME = ***  ,最终希望获取到C表中的详细信息 ,  首先查询到用户ID,关联到B表中 查出此用户的 所有标签ID,通过标签ID获取到详细信息
    ModelA
      'relab'=>array(self::HAS_MANY , 'ModelB' , array( 'A_ID' => 'ID' ) , 'with'=>'relbc' )    //模型A中查询B表中的数据

    ModelB
      'relbc'=>array(self::BELONGS_TO , 'ModelC' , array( 'TYPE_ID'=> 'ID' ) )   //通过B模型 关联查询C表中的数据

    $result = ModelA -> find(‘NAME=:NAME’,array(':NAME' => '***' )) -> with('relab');
     
    最终调用:
    由于是一对多的关系,所以   $result -> relab   一般是一个对象数组,这样我们就不能直接用  $result -> relab -> relbc 来调用C表中的字段信息了
     不过foreach 循环一下就ok了, 具体情况具体操作吧 :)

    从B关联到C 为一一对应关系, 所以关联类型也可以使用 HAS_ONE,  不过在对应参数就需要作对应的小修改了
    //HAS_ONE写法  'relbc'=>array(self::HAS_ONE , 'ModelC' , array( 'ID'=> 'TYPE_ID' ) )

    //以下为framework/db/ar/CActiveRecord.php 中节选的部分注释,实际使用过程中可作参考
    This method should be overridden to declare related objects.

    There are four types of relations that may exist between two active record objects:
    <ul>
    <li>BELONGS_TO: e.g. a member belongs to a team;</li>
    <li>HAS_ONE: e.g. a member has at most one profile;</li>
    <li>HAS_MANY: e.g. a team has many members;</li>
    <li>MANY_MANY: e.g. a member has many skills and a skill belongs to a member.</li>
    </ul>

    Besides the above relation types, a special relation called STAT is also supported
    that can be used to perform statistical query (or aggregational query).
    It retrieves the aggregational information about the related objects, such as the number
    of comments for each post, the average rating for each product, etc.

    Each kind of related objects is defined in this method as an array with the following elements:
    <pre>
    'varName'=>array('relationType', 'className', 'foreignKey', ...additional options)
    </pre>
    where 'varName' refers to the name of the variable/property that the related object(s) can
    be accessed through; 'relationType' refers to the type of the relation, which can be one of the
    following four constants: self::BELONGS_TO, self::HAS_ONE, self::HAS_MANY and self::MANY_MANY;
    'className' refers to the name of the active record class that the related object(s) is of;
    and 'foreignKey' states the foreign key that relates the two kinds of active record.
    Note, for composite foreign keys, they can be either listed together, separated by commas or specified as an array
    in format of array('key1','key2'). In case you need to specify custom PK->FK association you can define it as
    array('fk'=>'pk'). For composite keys it will be array('fk_c1'=>'pk_c1','fk_c2'=>'pk_c2').
    For foreign keys used in MANY_MANY relation, the joining table must be declared as well
    (e.g. 'join_table(fk1, fk2)').

    Additional options may be specified as name-value pairs in the rest array elements:
    <ul>
    <li>'select': string|array, a list of columns to be selected. Defaults to '', meaning all columns.
      Column names should be disambiguated if they appear in an expression (e.g. COUNT(relationName.name) AS name_count).</li>
    <li>'condition': string, the WHERE clause. Defaults to empty. Note, column references need to
      be disambiguated with prefix 'relationName.' (e.g. relationName.age&gt;20)</li>
    <li>'order': string, the ORDER BY clause. Defaults to empty. Note, column references need to
      be disambiguated with prefix 'relationName.' (e.g. relationName.age DESC)</li>
    <li>'with': string|array, a list of child related objects that should be loaded together with this object.
      Note, this is only honored by lazy loading, not eager loading.</li>
    <li>'joinType': type of join. Defaults to 'LEFT OUTER JOIN'.</li>
    <li>'alias': the alias for the table associated with this relationship.
      It defaults to null,
      meaning the table alias is the same as the relation name.</li>
    <li>'params': the parameters to be bound to the generated SQL statement.
      This should be given as an array of name-value pairs.</li>
    <li>'on': the ON clause. The condition specified here will be appended
      to the joining condition using the AND operator.</li>
    <li>'index': the name of the column whose values should be used as keys
      of the array that stores related objects. This option is only available to
      HAS_MANY and MANY_MANY relations.</li>
    <li>'scopes': scopes to apply. In case of a single scope can be used like 'scopes'=>'scopeName',
      in case of multiple scopes can be used like 'scopes'=>array('scopeName1','scopeName2').
      This option has been available since version 1.1.9.</li>
    </ul>

    The following options are available for certain relations when lazy loading:
    <ul>
    <li>'group': string, the GROUP BY clause. Defaults to empty. Note, column references need to
      be disambiguated with prefix 'relationName.' (e.g. relationName.age). This option only applies to HAS_MANY and MANY_MANY relations.</li>
    <li>'having': string, the HAVING clause. Defaults to empty. Note, column references need to
      be disambiguated with prefix 'relationName.' (e.g. relationName.age). This option only applies to HAS_MANY and MANY_MANY relations.</li>
    <li>'limit': limit of the rows to be selected. This option does not apply to BELONGS_TO relation.</li>
    <li>'offset': offset of the rows to be selected. This option does not apply to BELONGS_TO relation.</li>
    <li>'through': name of the model's relation that will be used as a bridge when getting related data. Can be set only for HAS_ONE and HAS_MANY. This option has been available since version 1.1.7.</li>
    </ul>

    Below is an example declaring related objects for 'Post' active record class:
    <pre>
    return array(
        'author'=>array(self::BELONGS_TO, 'User', 'author_id'),
        'comments'=>array(self::HAS_MANY, 'Comment', 'post_id', 'with'=>'author', 'order'=>'create_time DESC'),
        'tags'=>array(self::MANY_MANY, 'Tag', 'post_tag(post_id, tag_id)', 'order'=>'name'),
    );
    </pre>

    @return array list of related object declarations. Defaults to empty array.
click
©2010-2024 Jeen All Rights Reserved.Powered by emlog 京ICP备15058100号-1