我尝试在 sqlalchemy 查询中进行排序,参数来自包含排序参数列表(字段和方向)的“query_sort”。
这是代码
def select_all(self, query_paging, query_sort):
""" method to select all the transport type"""
try:
select_all_query =\
self._session.query(TransportType)
for s in query_sort:
select_all_query =\
>> select_all_query.order_by(s.dir(getattr(TransportType, s.field)))\
.limit(query_paging.page_size)\
.offset(query_paging.skip)\
.all()
return select_all_query
except NoResultFound:
return None
Run Code Online (Sandbox Code Playgroud)
然后在 py.test 中,我尝试使用以下代码测试该程序:
s1 = sort
s1.field = "type"
s1.dir = asc
s2 = sort
s2.field = "transport_type_id"
s2.dir = asc
query_sort = [s1,s2]
query_paging.skip = 1
query_paging.page_size = 10
transport_types = repo.select_all(query_paging, query_sort)
assert len(transport_types) == 1
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,出现此错误:
E AttributeError: 'list' …Run Code Online (Sandbox Code Playgroud) 我打算使用 Python27 中的 sqlalchemy 将数据提交到 mysql 表。当我尝试运行此文件时,它显示这样的错误
sqlalchemy.orm.exc.FlushError: Instance <TravelScheduleDetailRepository at 0x7f0fc07c8950> has a NULL identity key. If this is an auto-generated value, check that the database table allows generation of new primary key values, and that the mapped Column object is configured to expect these generated values. Ensure also that this flush() is not occurring at an inappropriate time, such aswithin a load() event.
Run Code Online (Sandbox Code Playgroud)
之后它成功地在 mysql 表上创建了一个新行,尽管所有列都为空
这是我的课
class TravelScheduleDetailRepository(Base):
__tablename__ = 'Schedule_Detail'
schedule_id = Column(String(7), primary_key = True) …Run Code Online (Sandbox Code Playgroud) 我试图像这样声明一个包含Datetime的变量
ts1.departure_date = '2012-03-03 10:10:10'
Run Code Online (Sandbox Code Playgroud)
但后来我收到了这个错误
StatementError: (exceptions.TypeError) SQLite DateTime type only accepts Python datetime and date objects as input.
Run Code Online (Sandbox Code Playgroud)
我想知道用datetime格式声明变量的正确方法是什么?提前致谢
您好我有这个名为save_schedule的方法,它将一些参数(transport_id,departure_id等)保存到数据库中.但是当我运行代码时,我得到了以下错误
newSchedule = TravelScheduleDetailRepository(self.transport_id=transport_id, self.transport_type=transport_type, self.transport_company_name=transport_company_name, self.departure_city_id=departure_city_id, self.departure_country_id=departure_country_id, self.destination_city_id=destination_city_id, self.destination_country_id=destination_country_id, self.departure_date=departure_date, self.available_seat=available_seat)
SyntaxError: keyword can't be an expression
Run Code Online (Sandbox Code Playgroud)
我仍然无法找到如何正确分配参数.
这是我的代码:
def save_travel_schedule(self, transport_id, transport_type, transport_company_name, departure_city_id, departure_country_id, destination_city_id, destination_country_id, departure_date, available_seat):
newSchedule = TravelScheduleDetailRepository(self.transport_id=transport_id, self.transport_type=transport_type, self.transport_company_name=transport_company_name, self.departure_city_id=departure_city_id, self.departure_country_id=departure_country_id, self.destination_city_id=destination_city_id, self.destination_country_id=destination_country_id, self.departure_date=departure_date, self.available_seat=available_seat)
session.add(newSchedule)
session.commit()
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.谢谢!
我正在尝试从radioButton获取字符串值,但是当我尝试它时会在LogCat上显示此错误.我似乎无法弄清楚如何解决它......这些是代码:
protected void onCreate(Bundle konsumsi) {
super.onCreate(konsumsi);
radioAktivitasGroup = (RadioGroup) findViewById(R.id.radioAktivitasGroup);
setContentView(R.layout.aktivitas_user);
usernameP = Login.usernameP;
System.out.println(usernameP);
buttonDone();
}
public void buttonDone() {
selesai = (Button) findViewById(R.id.buttonProses);
selesai.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int selectedId = radioAktivitasGroup.getCheckedRadioButtonId();
radioAktivitasButton = (RadioButton) findViewById(selectedId);
String Aktivitas = radioAktivitasButton.getText().toString();
}
});
}
Run Code Online (Sandbox Code Playgroud)
错误:
05-20 07:01:07.858: E/AndroidRuntime(3179): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ta.nutrifact/com.ta.nutrifact.AktivitasUser}: java.lang.NullPointerException
05-20 07:01:07.858: E/AndroidRuntime(3179): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-20 07:01:07.858: E/AndroidRuntime(3179): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-20 07:01:07.858: E/AndroidRuntime(3179): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-20 07:01:07.858: E/AndroidRuntime(3179): at …Run Code Online (Sandbox Code Playgroud) 我有一个函数,它接受page_size和skip作为参数.如果page_size = 10和skip = 2,我想从第21行开始选择10行.我认为这与LIMIT和OFFSET.我如何在SQLAlchemy中执行此操作?