如何在SQL中添加过滤器以从某列中选择NOT NULL值?
SELECT *
FROM table
WHERE YourColumn IS NOT NULL;
如何使用SQLAlchemy过滤器执行相同的操作?
select = select(table).select_from(table).where(all_filters)
答案 0 :(得分:104)
column_obj != None
将生成IS NOT NULL
constraint:
在列上下文中,生成子句
a != b
。如果目标是None
,则生成IS NOT NULL
。
或使用isnot()
(0.7.9中的新内容):
实施
IS NOT
运营商。通常,与
IS NOT
的值进行比较时会自动生成None
,该值会解析为NULL
。但是,如果与某些平台上的布尔值进行比较,则可能需要明确使用IS NOT
。
演示:
>>> from sqlalchemy.sql import column
>>> column('YourColumn') != None
<sqlalchemy.sql.elements.BinaryExpression object at 0x10c8d8b90>
>>> str(column('YourColumn') != None)
'"YourColumn" IS NOT NULL'
>>> column('YourColumn').isnot(None)
<sqlalchemy.sql.elements.BinaryExpression object at 0x104603850>
>>> str(column('YourColumn').isnot(None))
'"YourColumn" IS NOT NULL'
答案 1 :(得分:65)
从版本0.7.9开始,您可以使用过滤器运算符.isnot
而不是比较约束,如下所示:
query.filter(User.name.isnot(None))
只有在关注pep8时才需要此方法。
答案 2 :(得分:20)
如果其他人想知道,您可以使用
public class RegisterActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private EditText mUsernameField;
private EditText mPasswordField;
private EditText mConfirmPasswordField;
private Button mRegisterButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mUsernameField = (EditText) findViewById(R.id.enterUsername);
mPasswordField = (EditText) findViewById(R.id.enterPassword);
mConfirmPasswordField = (EditText) findViewById(R.id.confirmPassword);
mRegisterButton = (Button) findViewById(R.id.confirmRegistration);
mRegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createAccount(mUsernameField.getText().toString(), mPasswordField.getText().toString());
}
});
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d("account", "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d("account", "onAuthStateChanged:signed_out");
}
// ...
}
};
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
private void createAccount(String email, String password) {
Log.d("account", "createAccount:" + email);
if (!validateForm()) {
return;
}
// [START create_user_with_email]
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
Log.d("account", "createUserWithEmail:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(RegisterActivity.this, R.string.register_failed,
Toast.LENGTH_SHORT).show();
}
else {
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
RegisterActivity.this.startActivity(intent);
finish();
}
}
});
// [END create_user_with_email]
}
private boolean validateForm() {
boolean valid = true;
String email = mUsernameField.getText().toString();
if (TextUtils.isEmpty(email)) {
mUsernameField.setError("Required.");
valid = false;
} else {
mUsernameField.setError(null);
}
String password = mPasswordField.getText().toString();
if (TextUtils.isEmpty(password)) {
mPasswordField.setError("Required.");
valid = false;
} else {
mPasswordField.setError(null);
}
String confirmPassword = mConfirmPasswordField.getText().toString();
if (TextUtils.isEmpty(confirmPassword)) {
mConfirmPasswordField.setError("Required.");
valid = false;
} else {
mConfirmPasswordField.setError(null);
}
if(!confirmPassword.equals(password)) {
mConfirmPasswordField.setError("Passwords must be the same.");
valid = false;
}
return valid;
}
}
生成is_
:
>>> from sqlalchemy.sql import column >>> print column('foo').is_(None) foo IS NULL >>> print column('foo').isnot(None) foo IS NOT NULL
答案 3 :(得分:0)
对我来说,如果我执行以下操作,那么我将获得预期的行为:
query = query.filter(Model1.column1 == None)
如果我执行以下操作,则似乎未应用该过滤器:
query = query.filter(Model1.column1 is None)