我正在编写一个简单的脚本,用于检查用户帐户是否即将到期。我在UPDATE查询时遇到问题-基本上不会更新。我在互联网上找到的所有示例似乎都使用元组来更新行,但是我的案例要求参数彼此分开。
我对Python完全陌生(我从昨天开始)。我的数据库是MySQL(网络上几乎所有示例都使用SQLite),我无法更改它。我使用Python 3,并且服务器在Ubuntu 18.04上运行。我尝试将%s替换为?或:variable。我还尝试了这样做的不安全方式(易受SQL注入攻击),而且也没有用。
这是我当前的代码:
export default {
props: [],
data() {
return {
indexx: 0,
loading: true,
items: [],
pageTitle: '',
id: this.$route.params.id,
search: this.$route.params.search,
pagination: {
isLoading: true
}
};
},
computed: {
apiUrl() {
return this.$store.getters.apiUrl;
},
module() {
return this.$store.getters.module;
},
title() {
return this.$store.getters.title;
}
},
mounted: function() {
this.getItems().then(() => {
this.loading = false;
});
},
methods: {
getItems(page = 1) {
return new Promise((resolve) => {
this.setPaginationLoading();
this.$http.get(this.getUrl(page)).then((response) => {
this.items = response.data.data;
this.setPagination(response.data);
resolve();
}, () => {
this.$swal("Something went wrong. Try again!", '', "error");
});
});
},
getUrl(page = 1) {
if (this.module == 'users') {
if (this.search)
let query= '&search=' + this.search;
console.log('In nationality ', nation);
return this.$store.getters.apiUrl + this.module + '?page=' +
page + query;
}else
return this.$store.getters.apiUrl + this.module + '/?page=' +
page;
},
setPaginationLoading() {
this.pagination.isLoading = true;
},
setPagination(data) {
this.pagination = {
currentPage: data.meta.current_page,
from: data.meta.from,
lastPage: data.meta.last_page,
to: data.meta.to,
total: data.meta.total,
isLoading: false
}
},
changePage(page) {
this.getItems(page);
},
}
我希望它使用#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import mysql.connector
from mysql.connector import Error
import datetime
try:
mydb = mysql.connector.connect(
host="localhost",
user="rootery-root",
passwd="example",
database="playground"
)
sqlCursor = mydb.cursor()
sqlCursor.execute("SELECT id, email, last_email_date FROM users WHERE soon_expires = 1")
sqlResult = sqlCursor.fetchall()
setLastMailCmd = """UPDATE users SET last_email_date =%s WHERE id =%s"""
today = datetime.date.today()
for i in range(0, len(sqlResult)):
id = sqlResult[i][0]
email = sqlResult[i][1]
lastemaildate = sqlResult[i][2]
daydiff = lastemaildate - today
setLastMail = sqlCursor.execute(setLastMailCmd, (id, today))
if daydiff.days >= 30:
print "Sending mail and updating \"last_email_date\"."
setLastMail
else:
print "%s already received an e-mail this month - ignored." % email
setLastMail # debugging purposes
except mysql.connector.Error as error:
print("SQL connection error.".format(error))
finally:
if (mydb.is_connected()):
sqlCursor.close()
mydb.close()
print("Disconnected from database.")
print(today)
循环提供的数据来更新我的表,但是它什么也不做。
答案 0 :(得分:0)
尝试更多使用功能。您将所有内容都放在一个地方,调试起来并不容易。
您的问题是您使用setLastMail # debugging purposes
的方式。它什么也没做。
有什么更好的方法:
mydb = mysql.connector.connect(
host="localhost",
user="rootery-root",
passwd="example",
database="playground"
)
sqlCursor = mydb.cursor()
def set_last_email(id):
stmt = """UPDATE users SET last_email_date =%s WHERE id =%s"""
today = datetime.date.today()
sqlCursor.execute(stmt, (id, today))
然后只需执行您的set_last_email(id)
。
请记住要使游标成为全局游标,否则它在您的函数中将不可用。或直接从全局连接中的函数中获取它。
这当然是一个虚拟的示例,但是您需要从某个地方开始:)