我想为JSON数据usign python 3 google colab创建SQL表。 这是代码:
from sqlalchemy import Column, Integer, String, Text, DateTime, Float, Boolean, ForeignKey
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
total_data = [
{
"fields": {
"bkdate": None,
"business_credit_card_total_balances": None,
"business_credit_card_total_limits": None,
"business_total_monthly_debt_payments": None,
"business_total_mortgage_monthly_payments": None,
"created_at": "2016-08-04T00:29:03.067Z",
"detail_results": None,
"error_reason": "no reasons",
"fico_v2": "695",
"fico_v3": None,
"loanapp_id": 194,
"personal_credit_card_total_balances": None,
"personal_credit_card_total_limits": None,
"personal_total_monthly_payments": None,
"report_type": "CreditProfile",
"result": True,
"role": "applicant",
"total_mortgage_monthly_payments": None,
"username": "cho",
"version": "CF Microloan"
},
"model": "common.prequalresult",
"pk": 1
}
]
engine = create_engine('mysql://user:password@localhost:5432/my_data', echo=False)
Base = declarative_base()
class Article(Base):
__tablename__ = 'article'
id = Column(Integer, primary_key=True, autoincrement=True)
pk = Column(Integer, primary_key=False)
model = Column(String(100), nullable=True)
child = relationship('Comment', backref='article', uselist=False)
class Comment(Base):
__tablename__ = 'comment'
id = Column(Integer, primary_key=True, autoincrement=True)
bkdate = Column(String(100), nullable=True)
business_credit_card_total_balances = Column(Integer, nullable=True)
business_credit_card_total_limits = Column(Integer, nullable=True)
business_total_monthly_debt_payments= Column(Integer, nullable=True)
business_total_mortgage_monthly_payments= Column(Integer, nullable=True)
detail_resultsColumn(String(100), nullable=True)
created_at = Column(String(100), nullable=True)
error_reason = Column(String(100), nullable=True)
fico_v2= Column(String(100), nullable=True)
fico_v3= Column(String(100), nullable=True)
loanapp_id= Column(Integer, nullable=True)
personal_credit_card_total_balances= Column(Integer, nullable=True)
personal_credit_card_total_limits= Column(Integer, nullable=True)
personal_total_monthly_payments= Column(Integer, nullable=True)
report_type= Column(String(100), nullable=True)
result= Column(Integer, nullable=True)
role= Column(String(100), nullable=True)
total_mortgage_monthly_payments= Column(Integer, nullable=True)
username= Column(String(100), nullable=True)
version= Column(String(100), nullable=True)
article_id = Column(Integer, ForeignKey('article.id'))
x = Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
for temp_data in total_data:
pk = temp_data['pk']
model = temp_data['model']
bkdate = temp_data['fields']['bkdate']
business_credit_card_total_balances = temp_data['fields']['business_credit_card_total_balances']
created_at = temp_data['fields']['created_at']
business_credit_card_total_limits = temp_data['fields']['business_credit_card_total_limits']
business_total_monthly_debt_payments = temp_data['fields']['business_total_monthly_debt_payments']
business_total_mortgage_monthly_payments = temp_data['fields']['business_total_mortgage_monthly_payments']
detail_resultsColumn = temp_data['fields']['detail_resultsColumn']
error_reason = temp_data['fields']['error_reason']
fico_v2 = temp_data['fields']['fico_v2']
fico_v3 = temp_data['fields']['fico_v3']
loanapp_id = temp_data['fields']['loanapp_id']
personal_credit_card_total_balances = temp_data['fields']['personal_credit_card_total_balances']
personal_credit_card_total_limits = temp_data['fields']['personal_credit_card_total_limits']
personal_total_monthly_payments = temp_data['fields']['personal_total_monthly_payments']
report_type = temp_data['fields']['report_type']
result = temp_data['fields']['result']
role = temp_data['fields']['role']
total_mortgage_monthly_payments = temp_data['fields']['total_mortgage_monthly_payments']
username= temp_data['fields']['username']
version = temp_data['fields']['version']
article_id = temp_data['fields']['article_id']
parent1 = Article(pk=temp_data['pk'], model=model)
child = Comment(bkdate=bkdate,
business_credit_card_total_balances=business_credit_card_total_balances,
created_at=created_at,
business_credit_card_total_limits=business_credit_card_total_limits,
business_total_monthly_debt_payments=business_total_monthly_debt_payments,
business_total_mortgage_monthly_payments=business_total_mortgage_monthly_payments,
detail_resultsColumn=detail_resultsColumn,
error_reason=error_reason,
fico_v2=fico_v2,
fico_v3=fico_v3,
loanapp_id=loanapp_id,
personal_credit_card_total_balances=personal_credit_card_total_balances,
personal_credit_card_total_limits=personal_credit_card_total_limits,
personal_total_monthly_payments=personal_total_monthly_payments,
report_type=report_type,
result=result,
role=role,
total_mortgage_monthly_payments=total_mortgage_monthly_payments,
username=username,
version=version,
article_id=article_id,
article=parent1)
session.add(parent1)
session.add(child)
session.commit()
当我执行上面的代码时,它显示错误:ModuleNotFoundError:没有名为'MySQLdb'的模块。 该代码包括数据并使用SQLAlchemy ORM创建表。有没有人有其他方法可以为数据创建SQL表?
我使用:
安装了模块!pip install PyMySQL
我不知道如何解决它,有人可以帮忙吗?
答案 0 :(得分:0)
下面是在colab中使用mysql的示例。
您可以使其适应您的问题。它使用PyMySQL代替MySQLdb。
# install, set connection
!apt-get install mysql-server > /dev/null
!service mysql start
!mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root'"
!pip -q install PyMySQL
%load_ext sql
%config SqlMagic.feedback=False
%config SqlMagic.autopandas=True
%sql mysql+pymysql://root:root@/
# query using %sql or %%sql
df = %sql SELECT Host, User, authentication_string FROM mysql.user
df
答案 1 :(得分:0)
pip3 install pymysql
这是您的连接uri语法应为:
'mysql+pymysql://root:password@localhost:3306/myDb'