会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132647个问题
Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 182楼

import pymysql


class Mydb:
    config={
        "host":"localhost",
        "user":"root",
        "password":"root",
        "db":"music_project"
    }
    def __init__(self):
        self.connection=pymysql.connect(**Mydb.config)
        self.cursor=self.connection.cursor()
    def close(self):
        if self.connection:
            self.connection.close()
        if self.cursor:
            self.cursor.close()
    #执行插入 修改 删除
    def exeDML(self,sql,*args):
        try:
            #执行sql
            count=self.cursor.execute(sql,args)
            #提交事务
            self.connection.commit()    
            return count

        except Exception as e:
            print(e)
            if self.connection:
                self.connection.rollback()
        finally:
            self.close()
    def query_one(self,sql,*args):
        try:
            self.cursor.execute(sql,args)
            #获取结果集
            return self.cursor.fetchone()
        except Exception as e:
            print(e)
        finally:
            self.close()
    def query_all(self,sql,*args):
        try:
            self.cursor.execute(sql,args)
            #获取结果集
            return self.cursor.fetchall()
        except Exception as e:
            print(e)
        finally:
            self.close()
        
 
if __name__ == "__main__":
    dbutil=Mydb()
from dbutil import Mydb
class MyService:
    def login(self,uname,password):
        sql="select * from t_user where uname=%s and password=%s"
        user=Mydb.query_one(sql,uname,password)
        if user:
            return True
        else:
            return False
#登陆界面
from Myservice import MyService
if __name__=="__main__":
    uname=input("请输入用户名:")
    password=input("请输入密码:")
    myservice=MyService()
    if myservice.login(uname,password):
        print("登陆成功")
    else:
        print("登陆失败")

image.png

老师 你帮我看下我这个 是哪里有问题

Python 全系列/第五阶段:数据库编程/项目-音乐播放器-旧 184楼
Python 全系列/第五阶段:数据库编程/项目-音乐播放器 186楼
Python 全系列/第五阶段:数据库编程/mysql的使用 187楼
Python 全系列/第五阶段:数据库编程/mysql的使用 189楼

Enter password: ************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 5.7.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create table departments(department_id int,department_name varchar(30),location_id int);
ERROR 1046 (3D000): No database selected
mysql> create database emp default character set utf8;
Query OK, 1 row affected (0.00 sec)

mysql> use database;
ERROR 1049 (42000): Unknown database 'database'
mysql> use emp;
Database changed
mysql> create table bjsxt (employee_id int,employee_name varchar(10),employee_salary float98,2));
    ->
    -> create table bjsxt (employee_id int,employee_name varchar(10),employee_salary float(8,2));
    -> use emp;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'float98,2));

create table bjsxt (employee_id int,employee_name varchar(10' at line 1
mysql> create table bjsxt (employee_id int,employee_name varchar(10),employee_salary float(8,2));
Query OK, 0 rows affected (0.03 sec)

mysql> create   table    departments(department_id int,department_name varchar(30),location_id int);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(30),location_id int)' at line 1
mysql> use emp;
Database changed
mysql> create   table    departments(department_id int,department_name varchar(30),location_id int);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(30),location_id int)' at line 1
mysql> show tables;
+---------------+
| Tables_in_emp |
+---------------+
| bjsxt         |
+---------------+
1 row in set (0.00 sec)

mysql> use emp;
Database changed
mysql> create table departments(department_id int,department_name varchar(30),location_id int);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(30),location_id int)' at line 1
mysql> show tables;
+---------------+
| Tables_in_emp |
+---------------+
| bjsxt         |
+---------------+
1 row in set (0.00 sec)

mysql>  create table departments(department_id int,department_name varchar(30),location_id int);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(30),location_id int)' at line 1
mysql> show tables;
+---------------+
| Tables_in_emp |
+---------------+
| bjsxt         |
+---------------+
1 row in set (0.00 sec)

mysql>

我这里创建第二个表时,系统提示错误,show tables不出意外,果然只有第一个表,找不出错误点在哪?63、.png



Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 191楼
Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 193楼
Python 全系列/第五阶段:数据库编程/python操作mysql 194楼
Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 195楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637