0

これが私のMysqlテーブルスキーマです

表: 予約列:

id  int(11) PK AI
apt_id  varchar(200) 
checkin_date    date 
checkout_date   date 
price   decimal(10,0) 
deposit decimal(10,0) 
adults  int(11) 
source_id   int(11) 
confirmationCode    varchar(100) 
client_id   int(11) 
booking_date    datetime 
note    mediumtext 
Related Tables:property (apt_id → apt_id)
booking_source (source_id → id)

私はpython .soを使用して値を挿入しようとしていますここで私がしたこと

sql = "INSERT INTO `nycaptBS`.`booking` (`apt_id`, `checkin_date`, `checkout_date`, `price`,`deposite` `adults`, `source_id`, `confirmationCode`, `client_id`, `booking_date`) VALUES ('%s','%s','%s','%s','%s','%d','%d','%s','%d','%s' )"   %  (self.apt_id,self.start_at,self.end_at,self.final_price,self.deposit,self.adults,self.source_id,self.notes,self.client_id,self.booking_date,self.notes)  
x.execute(sql)

しかし、上記のスクリプトの実行中にエラーが発生します。

    sql = "INSERT INTO `nycaptBS`.`booking` (`apt_id`, `checkin_date`, `checkout_date`, `price`,`deposite` `adults`, `source_id`, `confirmationCode`, `client_id`, `booking_date`) VALUES ('%s','%s','%s','%s','%s','%d','%d','%s','%d','%s' )"   %  (self.apt_id,self.start_at,self.end_at,self.final_price,self.deposit,self.adults,self.source_id,self.notes,self.client_id,self.booking_date,self.notes)  
TypeError: %d format: a number is required, not NoneType

文字列フォーマッタが正しくないと思います 助けてください。

4

1 に答える 1

0

booking_date、notes、source_id、(また、notes 値 2x を挿入していますか?) のいずれかのように見えますNone。挿入する前に各値を確認/検証できます。

また、文字列の書式設定ではなく、パラメーター化されたクエリを使用してください

通常、SQL 操作では Python 変数の値を使用する必要があります。安全ではないため、Python の文字列操作を使用してクエリを組み立てるべきではありません。これにより、プログラムが SQL インジェクション攻撃に対して脆弱になります (問題が発生する可能性のあるユーモラスな例については、http: //xkcd.com/327/を参照してください)。

代わりに、DB-API のパラメーター置換を使用してください。置く ?値を使用する場所のプレースホルダーとして、カーソルの execute() メソッドの 2 番目の引数として値のタプルを提供します。

何かのようなもの:

x.execute("INSERT INTO thing (test_one, test_two) VALUES (?, ?)", (python_var_one, python_var_two,))

于 2013-04-16T13:58:20.013 に答える