~feng-kylin/youker-assistant/youker-assistant

« back to all changes in this revision

Viewing changes to backends/youker-assistant-daemon/src/weather/database.py

  • Committer: kobe
  • Date: 2015-02-13 07:37:10 UTC
  • Revision ID: xiangli@ubuntukylin.com-20150213073710-0jyp02ilyi5njj10
Qt Version

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# -*- coding: utf-8 -*-
3
 
 
4
 
### BEGIN LICENSE
5
 
# Copyright (C) 2013 ~ 2014 National University of Defense Technology(NUDT) & Kylin Ltd
6
 
# Author: Kobe Lee
7
 
#
8
 
# This program is free software: you can redistribute it and/or modify it
9
 
# under the terms of the GNU General Public License version 3, as published
10
 
# by the Free Software Foundation.
11
 
#
12
 
# This program is distributed in the hope that it will be useful, but
13
 
# WITHOUT ANY WARRANTY; without even the implied warranties of
14
 
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15
 
# PURPOSE.  See the GNU General Public License for more details.
16
 
#
17
 
# You should have received a copy of the GNU General Public License along
18
 
# with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
### END LICENSE
20
 
 
21
 
import sqlite3
22
 
import os
23
 
import sys
24
 
reload(sys)
25
 
sys.setdefaultencoding("utf-8")
26
 
#from xdg import BaseDirectory as xdg
27
 
 
28
 
#CREATE_AREA = "create table area (id varchar(32) primary key, province varchar(64), \
29
 
#                city varchar(64), county varchar(64), pinyin varchar(64))"
30
 
#INSERT_AREA = "insert into area (id, province, city, county, pinyin) \
31
 
#        values('%s', '%s', '%s', '%s', '%s')"
32
 
#QUERY_AREA = "select * from area where id='%s'"
33
 
QUERY_COUNTY = "select county from area where province='%s' and city='%s'"
34
 
QUERY_ID = "select id from area where province='%s' and city='%s' and county='%s'"
35
 
#RESET_AREA = "delete from area"
36
 
#UPDATE_AREA = "update area set pinyin='%s' where id='%s'"
37
 
 
38
 
 
39
 
 
40
 
#CREATE_CITY = "create table city (province varchar(64), cityname varchar(64))"
41
 
#INSERT_CITY = "insert into city (province, cityname) \
42
 
#        values('%s', '%s')"
43
 
QUERY_CITY_LIST = "select cityname from city where province='%s'"
44
 
 
45
 
 
46
 
#WEATHER_CACHE_DIR = os.path.join(xdg.xdg_cache_home, "indicator-china-weather")
47
 
#CHN_CITY_LIST_FILE = os.path.join(os.path.abspath(os.path.curdir), "locations.txt")
48
 
#CITY_LIST_FILE = os.path.join(os.path.abspath(os.path.curdir), "city.txt")
49
 
 
50
 
#DB_PATH = os.path.join(os.path.abspath(os.path.curdir), "weather-app.db")
51
 
 
52
 
province_list = [
53
 
    '北京',
54
 
    '上海',
55
 
    '天津',
56
 
    '重庆',
57
 
    '黑龙江',
58
 
    '吉林',
59
 
    '辽宁',
60
 
    '内蒙古',
61
 
    '河北',
62
 
    '山西',
63
 
    '陕西',
64
 
    '山东',
65
 
    '新疆',
66
 
    '西藏',
67
 
    '青海',
68
 
    '甘肃',
69
 
    '宁夏',
70
 
    '河南',
71
 
    '江苏',
72
 
    '湖北',
73
 
    '浙江',
74
 
    '安徽',
75
 
    '福建',
76
 
    '江西',
77
 
    '湖南',
78
 
    '贵州',
79
 
    '四川',
80
 
    '广东',
81
 
    '云南',
82
 
    '广西',
83
 
    '海南',
84
 
    '香港',
85
 
    '澳门',
86
 
    '台湾'
87
 
]
88
 
 
89
 
class Database:
90
 
 
91
 
    def __init__(self):
92
 
        destFile = os.path.join('/var/lib/youker-assistant-daemon',"weather-app.db")
93
 
        if not os.path.exists(destFile):
94
 
            print "error with db file"
95
 
            return
96
 
        #工程调用
97
 
        self.connect = sqlite3.connect(destFile, check_same_thread=False)
98
 
        # 本地运行创建数据库
99
 
        # self.connect = sqlite3.connect(DB_PATH, check_same_thread=False)
100
 
        self.cursor = self.connect.cursor()
101
 
 
102
 
    def __del__(self):
103
 
        self.cursor.close()
104
 
        self.connect.close()
105
 
 
106
 
    # create table
107
 
#    def create_area_table(self):
108
 
#        self.cursor.execute(CREATE_AREA)
109
 
 
110
 
#    # reset table
111
 
#    def reset_area_table(self):
112
 
#        self.cursor.execute(RESET_AREA)
113
 
#        self.connect.commit()
114
 
 
115
 
#    def search_record(self, id):
116
 
#        self.cursor.execute(QUERY_AREA % (id))
117
 
#        res = self.cursor.fetchall()
118
 
#        if len(res) == 0:
119
 
#            return []
120
 
#        else:
121
 
#            return res
122
 
    def get_province_list(self):
123
 
        return province_list
124
 
 
125
 
    def search_counties(self, province, city):
126
 
        self.cursor.execute(QUERY_COUNTY % (province, city))
127
 
        res = self.cursor.fetchall()
128
 
        if len(res) == 0:
129
 
            return []
130
 
        else:
131
 
            return res
132
 
 
133
 
    def search_id(self, province, city, county):
134
 
        self.cursor.execute(QUERY_ID % (province, city, county))
135
 
        res = self.cursor.fetchall()
136
 
        if len(res) == 0:
137
 
            return []
138
 
        else:
139
 
            return res
140
 
 
141
 
    # insert data
142
 
#    def insert_area_data(self):
143
 
#        file = open(CHN_CITY_LIST_FILE, 'r')
144
 
#        for line in file.readlines():
145
 
#            if line not in (False, None, '') and ',' in line and ':' in line:
146
 
#                line_list = line.strip('\n').split(':')
147
 
#                city_list = line_list[0].split(',')
148
 
#                self.cursor.execute(INSERT_AREA % (line_list[1],city_list[0],city_list[1],city_list[2],line_list[2]))
149
 
#                self.connect.commit()
150
 
#        file.close()
151
 
 
152
 
 
153
 
 
154
 
 
155
 
    # create table
156
 
#    def create_city_table(self):
157
 
#        self.cursor.execute(CREATE_CITY)
158
 
 
159
 
#    # insert data
160
 
#    def insert_city_data(self):
161
 
#        file = open(CITY_LIST_FILE, 'r')
162
 
#        for line in file.readlines():
163
 
#            if line not in (False, None, '') and ',' in line:
164
 
#                line_list = line.strip('\n').split(',')
165
 
#                self.cursor.execute(INSERT_CITY % (line_list[0],line_list[1]))
166
 
#                self.connect.commit()
167
 
#        file.close()
168
 
 
169
 
    def search_city_table(self, province):
170
 
        self.cursor.execute(QUERY_CITY_LIST % (province))
171
 
        res = self.cursor.fetchall()
172
 
        if len(res) == 0:
173
 
            return []
174
 
        else:
175
 
            return res
176
 
 
177
 
if __name__ == "__main__":
178
 
    db = Database()
179
 
    # db.create_area_table()
180
 
    #db.insert_area_data()
181
 
 
182
 
    # db.create_city_table()
183
 
    # db.insert_city_data()
184
 
 
185
 
 
186