简体   繁体   English

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

[英]how to access sqlite3 database table values from javascript?

My task is to report a graph by accessing sqlite3 database table values. 我的任务是通过访问sqlite3数据库表值来报告图形。 So i created database in python and i used javascript to report a graph.In python, i fetched all values from database and stored in list. 所以我用python创建了数据库,并使用javascript报告了图表。在python中,我从数据库中获取了所有值并存储在列表中。 Now, my problem is i dono how to access python list values from javascript. 现在,我的问题是我不知道如何从javascript访问python列表值。 please help me.. 请帮我..

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()
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))

You should iterate over list and concatenate items to the result string. 您应该遍历列表并将项目连接到结果字符串。

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)

This is a very easy way to achieve the above task. 这是完成上述任务的一种非常简单的方法。

    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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM