Merhaba üstadlarım…
Flask ile küçük bir uygulama yapmaya çalışıyorum. Bunu sqlite kullanarak hallettim çok şükür.
Fakat takıldığım bir nokta var. Sqlite yerine sqlserver kullanmayı deniyorum. Fakat her zaman hata ile karşılaşıyorum. bir iki gündür uğraştım fakat işin içinden çıkamayınca sizlere danışmak istedim.
@app.route('/kayit', methods=['GET', 'POST'])
def kayit():
mevcut_kullanici = Kullanicilar.query.filter_by().first()
if request.method == 'POST':
kullanici_adi = request.form.get('kullanici')
yeni_kullanici = Kullanicilar.query.filter_by(kullanici_adi = kullanici_adi).first()
if yeni_kullanici:
flash('Kullanıcı Zaten Kayıtlı.', category = 'error')
elif len(kullanici_adi) < 4:
flash('Kullanıcı Adı 3 Karakterden Fazla Olmalı.', category = 'error')
else:
new_user = Kullanicilar(kullanici_adi = kullanici_adi)
db.session.add(new_user)
db.session.commit()
flash('Kullanıcı Eklendi!', category = 'success')
return redirect(url_for('kayit'))
kull = Kullanicilar.query
kayitliKullanicilar = [u.kullanicilari_getir() for u in kull]
return render_template('kayit.html', kayitliKullanicilar = kayitliKullanicilar, user = current_user)
{% extends "base.html" %} {% block title %}Kayıt Oluştur{% endblock %} {% block content %}
<style>
table{
width:100%;
}
label {
display: inline-flex;
margin-bottom: .5rem;
margin-top: .5rem;
}
.page-item.disabled .page-link {
color: #6c757d;
pointer-events: none;
cursor: auto;
background-color: #fff;
border-color: #dee2e6;
}
</style>
<script>
function kullaniciSil(id){
$("#dialogSil").dialog({
autoOpen: false,
height: 200,
width: 300,
modal: true,
buttons: {
"Sil" : function(){
$("#dialogSil").dialog("close");
fetch("/kullanici-sil", {
method: "POST",
body: JSON.stringify({ id: id }),
}).then((_res) => {
window.location.href = "/kayit";
});
},
"İptal" : function(){
$("#dialogSil").dialog("close");
}
}
});
$("#dialogSil").dialog("open");
}
</script>
<table><tr><td style="vertical-align: top;">
<form method="POST">
<h3 align="center">Kayıt Oluştur</h3>
<div class="form-group">
<label for="kullanici">Kullanıcı Adı</label>
<input type="text" class="form-control" id="kullanici" name="kullanici" placeholder="Kullanıcı Adı Girin" />
</div>
<br />
<button type="submit" class="btn btn-primary">Kaydet</button>
</form>
</td><td style="vertical-align: top;">
<div class="container">
<div class="row">
<div class="col-12">
<table id="kullanicilar" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Kullanıcı Adı</th>
</tr>
</thead>
<tbody>
{% for gelenKullanici in kayitliKullanicilar %}
<tr>
<td>{{ gelenKullanici.kullanici_adi }}</td>
<td><button class="btn" onclick="kullaniciSil({{ gelenKullanici.id }} );"><i class="fa fa-trash"></i></button></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</td></tr></table>
<div id="dialogSil" title="Kullanıcı Sil" style="display: none;">
Kullanıcıyı Silmek İstediğinize Emin misiniz?
</div>
{% endblock %}
[2023-08-11 08:26:01,907] ERROR in app: Exception on /kayit [POST]
Traceback (most recent call last):
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\engine\base.py", line 1819, in _execute_context
self.dialect.do_execute(
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\engine\default.py", line 732, in do_execute
cursor.execute(statement, parameters)
pyodbc.IntegrityError: ('23000', "[23000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot insert the value NULL into column 'id', table 'mnt02.dbo.kullanicilar'; column does not allow nulls. INSERT fails. (515) (SQLExecDirectW); [23000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The statement has been terminated. (3621)")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\flask\app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\flask\app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\flask\app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\flask\app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "Web.py", line 86, in kayit
db.session.commit()
File "<string>", line 2, in commit
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\orm\session.py", line 1451, in commit
self._transaction.commit(_to_root=self.future)
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\orm\session.py", line 829, in commit
self._prepare_impl()
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\orm\session.py", line 808, in _prepare_impl
self.session.flush()
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\orm\session.py", line 3383, in flush
self._flush(objects)
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\orm\session.py", line 3523, in _flush
transaction.rollback(_capture_exception=True)
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\util\langhelpers.py", line 70, in __exit__
compat.raise_(
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\util\compat.py", line 208, in raise_
raise exception
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\orm\session.py", line 3483, in _flush
flush_context.execute()
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\orm\unitofwork.py", line 456, in execute
rec.execute(self)
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\orm\unitofwork.py", line 630, in execute
util.preloaded.orm_persistence.save_obj(
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\orm\persistence.py", line 245, in save_obj
_emit_insert_statements(
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\orm\persistence.py", line 1238, in _emit_insert_statements
result = connection._execute_20(
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\engine\base.py", line 1631, in _execute_20
return meth(self, args_10style, kwargs_10style, execution_options)
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\sql\elements.py", line 332, in _execute_on_connection
return connection._execute_clauseelement(
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\engine\base.py", line 1498, in _execute_clauseelement
ret = self._execute_context(
File "C:\Users\kaizen.admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\engine\base.py", line 1862, in _execute_context
self._handle_dbapi_exception(
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\engine\base.py", line 2043, in _handle_dbapi_exception
util.raise_(
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\util\compat.py", line 208, in raise_
raise exception
File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\engine\base.py", line 1819, in _execute_context
self.dialect.do_execute(
File "C:\Users\kadmin\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\engine\default.py", line 732, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.IntegrityError: (pyodbc.IntegrityError) ('23000', "[23000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot insert the value NULL into column 'id', table 'web.dbo.kullanicilar'; column does not allow nulls. INSERT fails. (515) (SQLExecDirectW); [23000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The statement has been terminated. (3621)")
[SQL: INSERT INTO kullanicilar (kullanici_adi) OUTPUT inserted.id VALUES (?)]
[parameters: ('Admin',)]
(Background on this error at: https://sqlalche.me/e/14/gkpj)