插入資料庫Geohash(haikou資料)

2021-10-05 03:55:45 字數 3105 閱讀 3827

from pygeohash import encode, decode

import plotly

import numpy as np

import pandas as pd

import math

from matplotlib.path import path

import numpy as np

import plotly.offline as of

import plotly.graph_objs as go

import chart_studio.plotly as py

import numpy as np

import pandas as pd

import folium

import webbrowser

from folium.plugins import heatmap

import datetime

import time

import pymysql.cursors

import decimal

import geohash

defmysql(id

):conn = pymysql.connect(

host=

'localhost'

, port=

3306

, user=

'root'

, passwd=

'xu19931026'

, db=

'hk_taxi'

, charset=

'utf8'

) cursor = conn.cursor(

)# 獲取游標

sql =

"select cast(starting_lng as char(11)) as beginlongitude,cast(starting_lat as char(10)) as beginlatitude from haikou_1 where id=%s"

# sql語句

cursor.execute(sql,id)

result=cursor.fetchall(

) df=

list

(result)

#將元組轉換為列表

lon =

lat =

conn.close(

)#關閉資料庫連線

for point in df:

float

(point[0]

))#將字串的經緯度轉換為float格式

float

(point[1]

))return lon,lat

defget_geohash

(lon, lat)

:#生成geohash

geo = geohash.encode(lat, lon)

# precision=9可以加精度

return geo

defget_geolist

(lon,lat)

:#根據mysql出的經緯度生成geohash

p =for i in

range

(len

(lon)):

result = get_geohash(lon[i]

,lat[i]

)return p

defgeohashsql

(geohash,id)

: conn = pymysql.connect(

host=

'localhost'

, port=

3306

, user=

'root'

, passwd=

'xu19931026'

, db=

'hk_taxi'

, charset=

'utf8'

) geohash=

(" "

.join(geohash)

)#列表轉換為字串

cursor = conn.cursor(

)# 獲取游標

sql =

"update haikou_1 set geo=%s where id=%s"

# sql語句

cursor.execute(sql,

(geohash,id)

) conn.commit(

)#不加這一句不執行

conn.close(

)# 關閉資料庫連線

#id最大12374604最小是1

#departure_time 從2017-04-29 07:20:00到2017-11-01 07:20:00

if __name__ ==

"__main__"

: time_start = time.time()id

=1000

while

id<

12374605

: lon,lat=mysql(id)

# 獲取mysql裡的經度,緯度,經度緯度組成的列表

geohash1=get_geolist(lon,lat)

# 根據經緯度獲得geohash1列表

geohashsql(geohash1,id)

id+=1if

id%50000==0

: time_end = time.time(

) yongshi=time_end-time_start

t=(12374604-id

)/id*yongshi

print

('已經完成'

+str(id

)+'**已用時**'

+str

(yongshi)

+'還需要'

+str

(t)+

'******'

)print

('處理完畢,請在資料庫中檢視'

)

scrapy 資料庫插入

1 安裝mysql sudo apt get install mysql 2 安裝python mysql sudo apt get install python mysqldb 3 安裝python支援mysql的驅動sudo pip install pymysql notice 安裝時密碼不要為...

插入資料庫操作

步驟如下 1 連線資料庫 conn pymysql.connect host user passwd db charset 2 建立操作游標,建立mysql的操作連線 cursor conn.cursor 3 寫sql語句 插入操作,方法有以下幾個 方法一 用format 注意項是若引數是字串形式,...

Golang web 資料庫 插入資料

go 語言中的 database sql 包定義了對資料庫的一系列操作。database sql driver包定義了應被資料庫驅動實現的介面,這些介面會被 sql 包使用。但是 go 語言沒有提供任何官方的資料庫驅動,所以我們需要匯入第三方的資料庫驅動。不過我們連線資料庫之後對資料庫操作的大部分 ...