如何在查询的(?)字符串中连接?或%s喜欢php ...我知道用+做但不是这样! 我需要查询像PHP一样更改字符串值吗?或%s(类似于Android Java)......
variableRange = ActiveSheet.Range("M1").Value
项目中所有模型的dbConnection层(Project中的所有模型都扩展了此类(通用))
package yyy.yyy.yyy.yyy.actions;
import android.content.Context;
import yyy.yyy.yyy.yyy.models.CategoriesModel;
public class CategoriesActions extends CategoriesModel {
protected String sqlInsert = "INSERT INTO categories (title) VALUES (?) ";
protected String sqlUpdate = "UPDATE categories SET title=? WHERE id=?";
protected String sqlDelete = "UPDATE FROM categories WHERE id=?";
protected String sqlSelect = "SELECT %s FROM categories %s %s %s"; // ? or %s
public CategoriesActions(Context context) {
super(context);
}
public boolean insert()
{
String cat_title = this.getTitle();
String sql = sqlInsert; // <---------- need concatenate cat_title in sqlInser (?)
//return sql;
return this.RunQuery(sql);
}
public String update()
{
String sql = "";
return sql;
}
public String delete()
{
String sql = "";
return sql;
}
public String select(String columns, String where, String order, String limit)
{
String sql = "";
return sql;
}
}
模型层(扩展dbConnection类(通用)
package yyy.yyy.yyy.yyy.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class dbConnection extends SQLiteOpenHelper {
private SQLiteDatabase conn;
public static final String DATABASE_NAME="App.Xitano";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_CATEGORIES = "categories";
private static final String TABLE_PAYMENT_TYPES = "payment_types";
private static final String TABLE_TRANSACTIONS = "transactions";
private static final String CATEGORIES_KEY_ID = "id";
private static final String CATEGORIES_KEY_TITLE = "title";
private static final String CATEGORIES_KEY_CREATED_AT = "created_at";
private static final String CATEGORIES_KEY_UPDATED_AT = "updated_at";
private static final String PAYMENT_TYPES_KEY_ID = "id";
private static final String PAYMENT_TYPES_KEY_TITLE = "title";
private static final String PAYMENT_TYPES_KEY_CREATED_AT = "created_at";
private static final String PAYMENT_TYPES_KEY_UPDATED_AT = "updated_at";
private static final String TRANSACTIONS_KEY_ID = "id";
private static final String TRANSACTIONS_KEY_IN_OUT = "in_out";
private static final String TRANSACTIONS_KEY_CATEGORIES_ID = "categories_id";
private static final String TRANSACTIONS_KEY_PAYMENT_TYPES_ID = "payment_types_id";
private static final String TRANSACTIONS_KEY_TITLE = "title";
private static final String TRANSACTIONS_KEY_AMOUNT = "amount";
private static final String CTRANSACTIONS_KEY_CREATED_AT = "created_at";
private static final String TRANSACTIONS_KEY_UPDATED_AT = "updated_at";
private static final String CREATE_TABLE_CATEGORIES = "CREATE TABLE " + TABLE_PAYMENT_TYPES + "("
+ PAYMENT_TYPES_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ PAYMENT_TYPES_KEY_TITLE + " TEXT,"
+ PAYMENT_TYPES_KEY_CREATED_AT + "TIMESTAMP,"
+ PAYMENT_TYPES_KEY_UPDATED_AT + "TIMESTAMP );";
private static final String CREATE_TABLE_PAYMENT_TYPES = "CREATE TABLE " + TABLE_CATEGORIES + "("
+ CATEGORIES_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ CATEGORIES_KEY_TITLE + " TEXT,"
+ CATEGORIES_KEY_CREATED_AT + "TIMESTAMP,"
+ CATEGORIES_KEY_UPDATED_AT + "TIMESTAMP );";
public dbConnection(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("table", TABLE_CATEGORIES);
}
@Override
public void onCreate(SQLiteDatabase db) {
/*
db.execSQL(CREATE_TABLE_STUDENTS);
db.execSQL(CREATE_TABLE_USER_HOBBY);
db.execSQL(CREATE_TABLE_USER_CITY);
*/
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
/*
db.execSQL("DROP TABLE IF EXISTS '" + TABLE_USER + "'");
db.execSQL("DROP TABLE IF EXISTS '" + TABLE_USER_HOBBY + "'");
db.execSQL("DROP TABLE IF EXISTS '" + TABLE_USER_CITY + "'");
onCreate(db);
*/
}
public SQLiteDatabase ConnectWritable(){
this.conn = this.getWritableDatabase();
return this.conn;
}
public SQLiteDatabase ConnectReadable(){
this.conn = this.getReadableDatabase();
return this.conn;
}
public boolean RunQuery(String sql){
SQLiteDatabase stm = this.ConnectWritable();
stm.execSQL(sql);
return true;
}
public Cursor RunSelect(String sql){
SQLiteDatabase stm = this.ConnectReadable();
Cursor cursor = stm.rawQuery(sql, null);
return cursor;
}
public int RunCount(String sql){
SQLiteDatabase stm = this.ConnectReadable();
Cursor cursor = stm.rawQuery(sql, null);
cursor.moveToFirst();
if (cursor.getCount() > 0 && cursor.getColumnCount() > 0) {
cursor.close();
return cursor.getInt(0);
} else {
cursor.close();
return 0;
}
}
public void Disconnect(){
this.conn.close();
this.conn = null;
}
}
谢谢你!
答案 0 :(得分:0)
你提到准备好的陈述“在这一层并不好”,但质疑为什么准备好的陈述对你不起作用?例如,在您的代码中,您有:
protected String sqlInsert = "INSERT INTO categories (title) VALUES (?) ";
您可以轻松创建预备声明:
PreparedStatement pstmt = con.prepareStatement(sqlInsert);
然后,您所要做的就是使用可用于预准备语句的set()方法将相关参数传递给查询。更多信息here。
答案 1 :(得分:0)
解决:
protected String sqlInsert = "INSERT INTO categories (title) VALUES (%s) ";
public boolean insert()
{
String sql = String.format(sqlInsert, this.getTitle());
return this.RunQuery(sql);
}
谢谢你,朋友们!