我在数据库中有一个表,包括列,部门名称,名称,ID,日期,日期,时间,手机号码。 每列都有自己的值。 我想从数据库中获取上述数据,并从数据库表中发送短信提取详细信息 短信格式为:姓名出席日期,日期为:时间 (发送至数据库中的否)
我们找到了将短信发送到文本文件的代码,并通过sim 300正常工作。 但我们希望它发送到nos。存储在数据库中 我们为文本文件设计的代码如下
import serial
import MySQLdb as mdb
import _mysql as m
def sendMsg(s,num,text):
cr = chr(13)
s.write('at' + cr)
s.write('AT+CMGF=1'+cr)
s.write('AT+CMGS="' + num + '"' + cr)
s.write(text)
s.write(cz)
s.write(cr)
print "Msg Sent"
#print s.readline()
#print s.readline()
#def sendComm(s,string):
# s.write(string + chr(13))
# print s.readline()
# print string,'--->',s.readline()
s = serial.Serial(19)
cz = chr(26)
s.setBaudrate(9600)
with open('palav.txt','r') as f:
lines = f.readlines()
for line in lines:
name,number,att = line.split(',')
att = att.strip()
text = "Your ward %s is having attendance %s%%" % (name,att)
print text
sendMsg(s,number,text)
print "Message Sent"
s.close()
请帮我们通过数据库发送 用户名:root,密码:""数据库 :出席
答案 0 :(得分:0)
#!/usr/bin/python
# view_rows.py - Fetch and display the rows from a MySQL database query
# import the MySQLdb and sys modules
import MySQLdb
import sys
# open a database connection
# be sure to change the host IP address, username, password and database name to match your own
connection = MySQLdb.connect (host = "192.168.1.2", user = "user", passwd = "password, db = "scripting_mysql")
# prepare a cursor object using cursor() method
cursor = connection.cursor ()
# execute the SQL query using execute() method.
cursor.execute ("select mobile_number from table ")
# fetch all of the rows from the query
data = cursor.fetchall ()
# print the rows
for row in data :
print row[0]// this ur mobile no --> add this to ur function send sms
// addd ur function here
# close the cursor object
cursor.close ()
# close the connection
connection.close ()
# exit the program
sys.exit()