问题列表 - 第42904页

我可以将我的sqlite连接和光标放在一个函数中吗?

我想我会尝试使我的sqlite数据库连接成为一个函数,而不是复制/粘贴连接和执行查询所需的~6行.我想让它变得多功能,所以我可以使用相同的功能来创建/选择/插入/等...

以下是我的尝试.'INSERT'和'CREATE TABLE'查询正在运行,但如果我执行'SELECT'查询,我如何处理它在函数外部获取的值?
通常我想打印它所取的值,也可以用它们做其他事情.

当我这样做时,我得到一个错误

Traceback (most recent call last):
File "C:\Users\steini\Desktop\py\database\test3.py", line 15, in <module>
for row in connection('testdb45.db', "select * from users"):
ProgrammingError: Cannot operate on a closed database.
Run Code Online (Sandbox Code Playgroud)

所以我猜连接需要打开,所以我可以从光标中获取值,但我需要关闭它,这样文件就不会总是被锁定.

这是我的测试代码:

import sqlite3

def connection (db, arg, cubby):
    conn = sqlite3.connect(db)
    conn.execute('pragma foreign_keys = on')
    cur = conn.cursor()
    cur.execute(arg)
    for row in cur:
        cubby.append(row)
    conn.commit()
    conn.close()

cubby=[]
connection('testdb.db', "create table users ('user', 'email')", cubby)
connection('testdb.db', "insert into users ('user', 'email') values ('joey', 'foo@bar')", cubby)
for row in connection('testdb45.db', …
Run Code Online (Sandbox Code Playgroud)

python sqlite oop

7
推荐指数
1
解决办法
1万
查看次数

BufferedInputStream.read(byte [])导致问题.以前有人有这个问题吗?

我写了一个Java程序,为我下载音频文件,我正在使用BufferedInputStream.read()函数工作正常,但实际上很慢,所以我尝试使用byte []来重载版本.

出于某种原因,下载后音频变得有损和奇怪.我不完全确定我做错了所以任何帮助都表示赞赏!这是代码的简化版本.

BufferedInputStream bin = new BufferedInputStream((new URL(url)).openConnection().getInputStream());
File file = new File(fileName);
FileOutputStream fop = new FileOutputStream(file);
int rd = bin.read();
while(rd != -1)
{
     fop.write(rd);
     rd = bin.read();
}
Run Code Online (Sandbox Code Playgroud)

java audio file-io

1
推荐指数
1
解决办法
1107
查看次数

哪个HTML5 Media JS框架最好?

我想使用jquery自定义视频播放器以获得额外的按钮和控件.我发现两个看起来像是可行的候选人,我有兴趣听到哪个更好(或者还有另一个最好的).请记住,我的目标是修改它(不只是删除和播放).

以下是我到目前为止找到的两位候选人:

  1. http://videojs.com/
  2. http://mediaelementjs.com/

media video jquery html5 canvas

5
推荐指数
1
解决办法
2515
查看次数

如何通过对象的属性对对象数组进行排序?

我在控制器中执行此查询,而"组"模型has_many用户

@group= Group.find(params[:id])
Run Code Online (Sandbox Code Playgroud)

@group用于呈现此部分(部分用户转储到表中)

<%= render :partial=>"user_list", :locals=>{:users=>@group.users} %>
Run Code Online (Sandbox Code Playgroud)

传递给partial的局部变量'users'是一个User对象数组;

- !ruby/object:User 
  attributes: 
    updated_at: 2011-01-04 21:12:04
    firstname:  Bob
    lastname: Smith
    id: "15"
    group_id: "2"
    created_at: 2010-11-26 12:54:45
Run Code Online (Sandbox Code Playgroud)

如何通过'lastname'对用户数组进行排序?没试过,我尝试了几种不同的方法.试图按照数组中的对象属性进行排序让我感到困惑.另外,我不知道如何通过以下方式执行此操作:查询中的顺序(如何:不是命令组而是命令每个组的用户)?

也许我没有正确引用对象的名称('用户')?看起来这应该是有效的,因为它会产生'无方法'错误(如果在没有!的情况下使用'sort_by',则会产生动态常量赋值错误):

 users.sort_by! {|User| User.lastname}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

ruby-on-rails

35
推荐指数
3
解决办法
5万
查看次数

使用linq拆分列表

我有以下代码:

  var e = someList.GetEnumerator();
  var a = new List<Foo>();
  var b = new List<Foo>();
  while(e.MoveNext())  {
     if(CheckCondition(e.Current)) {
         b.Add(e.Current);
         break;
     }
     a.Add(e.Current);
 }

while(e.MoveNext())
  b.Add(e.Current)
Run Code Online (Sandbox Code Playgroud)

这看起来很难看.基本上,遍历列表并将元素添加到一个列表中,直到某些条件启动,然后将其余内容添加到另一个列表中.

有没有更好的方法,例如使用linq?CheckCondition()很昂贵,列表可能很大,所以我宁愿不做任何迭代列表两次的事情.

c#

9
推荐指数
1
解决办法
2665
查看次数

让所有直接的孩子,没有更深层次

WebElement body = browser.findElement(By.xpath("//body"));

body.findElement(By.xpath("")); // I want to get all child elements 
                                // inside body, but nothing deeper.
Run Code Online (Sandbox Code Playgroud)

示例文档.

<html>
  <body>
    <div>
    </div>
    <span>
      <table>
      </table>
    </span>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

预期的结果是divspan.我无法控制文件,而且差异很大.

xpath webdriver

29
推荐指数
3
解决办法
3万
查看次数

使用谷歌adwords api

我需要做什么才能使用Google AdWords API?我是否必须在我的客户中心注册,付钱并通过谷歌测试服务的愚蠢测试?

google-adwords

4
推荐指数
1
解决办法
4018
查看次数

根据cron规范计算下一个预定时间

在给定当前时间和cron规格的情况下,计算事件的下一个运行时间的有效方法是什么?

我正在寻找除"每一分钟检查它是否符合规格"之外的其他内容.

规范示例可能是:

  • 每个月,1日和15日15:01
  • 每小时10,20,30,40,50分钟

Python代码很可爱,但是psuedo代码或高级描述也会受到赞赏.

[更新]假设规范已经解析并且格式合理.

python algorithm cron scheduler

14
推荐指数
1
解决办法
6497
查看次数

如何连接两个节点集以使订单得到尊重?

我的理解是,尽管XSLT的"节点集"被称为"集合",但它们实际上是有序的节点列表(这就是每个节点与索引相关联的原因).我因此一直试图使用"|" 运算符以连接节点集,以便遵守节点的顺序.

我试图完成的是类似下面的JavaScript代码:

[o1,o2,o3].concat([o4,o5,o6])
Run Code Online (Sandbox Code Playgroud)

产量:

[o1,o2,o3,o4,o5,o6]
Run Code Online (Sandbox Code Playgroud)

但是,请考虑以下简化示例:

testFlatten.xsl

<?xml version="1.0"?>
<xsl:stylesheet 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 version="1.0">
 <xsl:output method="xml"/>

 <xsl:template match="/">

  <xsl:variable name="parentTransition" select="//*[@id='parentTransition']"/>
  <xsl:variable name="childTransition" select="//*[@id='childTransition']"/>
  <xsl:variable name="parentThenChildTransitions" select="$parentTransition | $childTransition"/>
  <xsl:variable name="childThenParentTransitions" select="$childTransition | $parentTransition"/>

  <return>
   <parentThenChildTransitions>
    <xsl:copy-of select="$parentThenChildTransitions"/>
   </parentThenChildTransitions>
   <childThenParentTransitions>
    <xsl:copy-of select="$childThenParentTransitions"/>
   </childThenParentTransitions>
  </return>

 </xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

鉴于以下输入:

<?xml version="1.0"?>
<root>
        <element id="parentTransition"/>

 <element id="childTransition"/>
</root>
Run Code Online (Sandbox Code Playgroud)

产量(使用xsltproc):

<?xml version="1.0"?>
<return>
    <parentThenChildTransitions>
        <element id="parentTransition"/><element id="childTransition"/>
    </parentThenChildTransitions>
    <childThenParentTransitions>
        <element id="parentTransition"/><element id="childTransition"/>
    </childThenParentTransitions>
</return>
Run Code Online (Sandbox Code Playgroud)

所以"|" 实际上,运算符不遵循节点集操作数的顺序.有没有办法可以连接节点集,以便顺序得到尊重?

xslt xpath xpath-2.0

4
推荐指数
1
解决办法
6822
查看次数

'distcp'和'distcp -update'之间的区别?

有什么区别

hadoop distcp
Run Code Online (Sandbox Code Playgroud)

hadoop distcp -update
Run Code Online (Sandbox Code Playgroud)

他们两个都会做同样的工作,我们称他们的方式略有不同.它们都不会覆盖目标中已存在的文件.那么在两组不同的命令中有什么意义呢?

hadoop mapreduce hdfs

6
推荐指数
1
解决办法
1万
查看次数