如何从javascript访问sqlite3数据库表值?

时间:2015-03-20 13:27:04

标签: javascript python sqlite

我的任务是通过访问sqlite3数据库表值来报告图形。所以我在python中创建了数据库,并使用javascript来报告图形。在python中,我从数据库中获取所有值并存储在列表中。现在,我的问题是我不知道如何从javascript访问python列表值。请帮帮我..

import sqlite3
list = []
conn = sqlite3.connect('persistentautomation.db')
cursor = conn.execute("SELECT date, gb from memoryused")
for row in cursor:
    print "date :", row[0]
    print "gb :", row[1]
    list.append([row[0],row[1]])
def memory():
    return list

req = memory();
print req  #This is the list which i created

memory();
ff = open('chart.html','w')
msg='''
<html>
<head>
<script type="text/javascript"
src=\'https://www.google.com/jsapi?autoload={
    "modules":[{
      "name":"visualization",
      "version":"1",
      "packages":["corechart"]
    }]
   }\'></script>

<script type="text/javascript">
google.setOnLoadCallback(drawChart);

function drawChart() {
    var data = google.visualization.arrayToDataTable([             .

     #i want to pass that list variable here...help please..

    ]);

    var options = {
      title: "PERSISTENT AUTOMATION MEMORY USAGE REPORT",
      curveType: "function",
      legend: { position: "bottom" }
    };

    var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

    chart.draw(data, options);
}
</script>
</head>
<body>

<div id="curve_chart" style="width: 900px; height: 500px"></div>
</body>
</html>'''
ff.write(msg)
ff.close()

3 个答案:

答案 0 :(得分:1)

import sqlite3
from string import Template

conn = sqlite3.connect('persistentautomation.db')
cursor = conn.execute("SELECT date, gb from memoryused")

results = []

for row in cursor:
    results.append({'date': row[0], 'gb': row[1]})
    print "date :", row[0]
    print "gb :", row[1]

template = '''
<html>
<head>
<script type="text/javascript"
src=\'https://www.google.com/jsapi?autoload={
    "modules":[{
      "name":"visualization",
      "version":"1",
      "packages":["corechart"]
    }]
   }\'></script>

<script type="text/javascript">
google.setOnLoadCallback(drawChart);

function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ["Memory", "Usage (GB)"],
     $res
    ]);

    var options = {
      title: "PERSISTENT AUTOMATION MEMORY USAGE REPORT",
      curveType: "function",
      legend: { position: "bottom" }
    };

    var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

    chart.draw(data, options);
}
</script>
</head>
<body>

<div id="curve_chart" style="width: 900px; height: 500px"></div>
</body>
</html>'''

with open('chart.html', 'w') as html:
    data = ','.join(['["{date}", {gb}]'.format(**r) for r in results])
    html.write(Template(template).substitute(res=data))

答案 1 :(得分:0)

您应该遍历列表并将项连接到结果字符串。

items = ['apple', 'grape']

html_string = '''
<html> <body> <h1> My list </h1> <ul>{list}</ul> </body> </html>
'''

my_list = ''.join(['<li>{}</li>'.format(i) for i items])
html_string = html_string.format(list=my_list)

with open('test.html', 'w') as html:
    html.write(html_string)

答案 2 :(得分:0)

这是实现上述任务的一种非常简单的方法。

    import ast
    memory_used = "/Users/Shared/MemoryUsed.log"
    memory_used = []
    with open(memory_used) as f:
    for line in f:
        linesplit = line.split()
        date = linesplit[1]
        gbsplit = linesplit[2]
        gb = ast.literal_eval(gbsplit[0])
        memory_used.append([date,gb])

    print memory_used  #This is the list

    ff = open('chart_new_try.html','w')
    html1="""<html>
    <head>
    <script type="text/javascript"
      src="https://www.google.com/jsapi?autoload={
        'modules':[{
          'name':'visualization',
          'version':'1',
          'packages':['corechart']
        }]
      }"></script>


    <script type="text/javascript">
      google.setOnLoadCallback(drawChart);

    function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['Date', 'MemoryUsed'],"""

    html2 =  """ ]);

    var options = {
      title: 'GRAPH REPORT',
      curveType: 'function',
      legend: { position: 'bottom' }
    };

    var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

    chart.draw(data, options);
    }
    </script>
    </head>
    <body>
      <div id="curve_chart" style="width: 900px; height: 500px"></div>
    </body>
    </html>"""

    msg = html1 + memory_used + html2
    ff.write(msg)
    ff.close()