我在MATLAB中编写GUI并使用mksqlite
。我有问题将参数传递给它。
命令为mksqlite([ 'INSERT INTO ' , z , ' VALUES (?)' ], data);
,其中:
z = 'table name'
,'table name'
从列表框中选择。 data
来自inputdlg
。 问题在于?
参数,因为不同的表具有不同的列数。有人知道以动态方式声明?
的方法吗?
a = get(handles.listbox1,'String');
b = get(handles.listbox1,'Value');
tabela=a{b};
disp(tabela);
mksqlite( 'param_wrapping', 1 );
mksqlite( 'result_type', 1 );
[results,colnames] = mksqlite(['SELECT * FROM ', tabela])
disp(results);
e=fieldnames(results);
v=size(e);
for r=1:v
prompt={'Wprowadź '};
dlg_title = 'Wprowadź ';
num_lines = 1;
answer = inputdlg(prompt,dlg_title,num_lines);
imie=answer{1};
disp(imie);
t=answer{1};
data{r}=t;
disp(data);
z=a{b};
end;
mksqlite([ 'INSERT INTO ' , z , ' VALUES (?,?,?,?)' ], data); %// << this should be dynamic
答案 0 :(得分:0)
您的想法是,您应该动态创建 public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("Erro");
// Setting Dialog Message
alertDialog.setMessage("GPS não está ligado. Gostaria de Checar nas Configurações?");
// On pressing Settings button
alertDialog.setPositiveButton("Configurações", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
或为每个表格案例获取预定义语句。
您可以尝试使用VALUES(...)
- 到 - String
映射,其中键是表名,值是String
语句。这可以在MATLAB中使用containers.Map
对象实现。这是我的意思的一个例子:
假设您的表名是VALUES
,我们假设这些表分别有2,3和4个字段。因此,SQL命令的相应table_names = {'table1','table2','table3'};
部分将是:VALUES
现在我们从这些sql_values = {'VALUES (?,?)','VALUES (?,?,?)','VALUES (?,?,?,?)'};
组合中构建Map
:
String
构建values_map = containers.Map(table_names,sql_values);
后,您可以使用从列表框中获取的表名(代码中为Map
)并检索正确的a
语句:
VALUES
或者,您可以使用逻辑来确定在vals_str = values_map(a);
之后放入括号中的?,
的正确数量:
VALUES
注意:您可以在案例中保持不同的switch a
case 'table1'
nVals = 2;
case 'table2'
nVals = 3;
case 'table3'
nVals = 4;
end
vals_str = ['VALUES (' repmat('?,',[1 nVals-1]) '?)'];
,而无需在运行时构建vals_str
。我个人喜欢这样的版本,因为我无法解释。
最后,您在Strings
中使用vals_str
:
mksqlite