我想要Python代码帮助发送短信到存储在MySQL数据库的号码

0 投票
1 回答
848 浏览
提问于 2025-04-18 03:05

我在数据库里有一个表,里面有几个列,分别是部门名称、姓名、身份证号、日期、星期几、时间和手机号码。每一列都有自己的值。

我想从数据库中提取这些数据,然后发送一条短信,短信内容是:某人在某个日期、星期几的考勤时间是:某个时间 (发送到数据库里的号码)

我们找到了一个可以通过SIM 300发送短信到文本文件的代码,运行得很好。但我们想要的是把短信发送到存储在数据库里的号码。

我们为文本文件设计的代码如下:

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,密码是:“”,数据库名是:attend

1 个回答

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

撰写回答