我需要一个包含以下数据的工作数据库,我应该能够从中获取数据,按下按钮,请帮助
这是代码
package com.example.prog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.ContextMenu;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Locale;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button Button1;
Button Button2;
Button Button3;
Button Button4;
Button Button5;
Button Button6;
Button Button7;
Button Button8;
Button Button9;
Button Button10;
TextView textView1;
TextView textView2;
TextView textView3;
TextView textView4;
TextView textView5;
TextView textView6;
TextView textView7;
TextView textView8;
TextView textView9;
TextView textView10;
TextView textView11;
EditText editText1;
EditText editText2;
EditText editText3;
EditText editText7;
EditText editText5;
final int MENU_main_Q = 0;
final int MENU_main_Q_1 = 7;
final int MENU_main_Q_2 = 8;
final int MENU_main_Q_3 = 9;
final int MENU_main_G = 1;
final int MENU_main_G_1 = 10;
final int MENU_main_G_2 = 11;
final int MENU_main_G_3 = 12;
final int MENU_main_G_4 = 13;
final int MENU_main_G_5 = 14;
final int MENU_main_G_6 = 15;
double value_c = 6;
double value_k1 = 1;
double value_k2 = 1;
double value_density = 1;
final String LOG_TAG = "myLogs";
final int DB_VERSION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button1 = (Button) findViewById(R.id.button1);
Button2 = (Button) findViewById(R.id.button2);
Button3 = (Button) findViewById(R.id.button3);
Button4 = (Button) findViewById(R.id.button4);
Button5 = (Button) findViewById(R.id.button5);
Button6 = (Button) findViewById(R.id.button6);
Button7 = (Button) findViewById(R.id.button7);
Button8 = (Button) findViewById(R.id.button8);
Button9 = (Button) findViewById(R.id.button9);
Button10 = (Button) findViewById(R.id.button10);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
textView3 = (TextView) findViewById(R.id.textView3);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
editText5 = (EditText) findViewById(R.id.editText5);
editText7 = (EditText) findViewById(R.id.editText7);
Button1.setOnClickListener(this);
Button2.setOnClickListener(this);
Button3.setOnClickListener(this);
Button4.setOnClickListener(this);
Button5.setOnClickListener(this);
Button6.setOnClickListener(this);
Button7.setOnClickListener(this);
Button8.setOnClickListener(this);
Button9.setOnClickListener(this);
Button10.setOnClickListener(this);
Button7.setTextColor(Color.RED);
Button8.setTextColor(Color.WHITE);
Button9.setTextColor(Color.WHITE);
Button7.setBackgroundColor(Color.WHITE);
Button8.setBackgroundColor(Color.GRAY);
Button9.setBackgroundColor(Color.GRAY);
registerForContextMenu(textView1);
registerForContextMenu(textView2);
registerForContextMenu(textView3);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
switch (v.getId()) {
case R.id.textView2:
menu.add(0, MENU_main_Q_1, 0, "кВт");
menu.add(0, MENU_main_Q_2, 0, "мВт");
menu.add(0, MENU_main_Q_3, 0, "Вт");
break;
}
switch (v.getId()) {
case R.id.textView1:
menu.add(0, MENU_main_G_1, 0, "кг/ч");
menu.add(0, MENU_main_G_2, 0, "м3/ч");
menu.add(0, MENU_main_G_3, 0, "л/c");
menu.add(0, MENU_main_G_4, 0, "кг/с");
menu.add(0, MENU_main_G_5, 0, "м3/с");
menu.add(0, MENU_main_G_6, 0, "фут3/нед");
break;
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case MENU_main_Q_1:
textView2.setText("кВт");
value_k1 = 1;
break;
case MENU_main_Q_2:
textView2.setText("мВт");
value_k1 = 1000;
break;
case MENU_main_Q_3:
textView2.setText("Вт");
value_k1 = 0.001;
break;
case MENU_main_G_1:
textView1.setText("кг/ч");
value_k2 = 1;
break;
case MENU_main_G_2:
textView1.setText("м3/ч");
value_k2 = value_density;
break;
case MENU_main_G_3:
textView1.setText("л/с");
value_k2 = value_density * 0.001 / 3600;
break;
case MENU_main_G_4:
textView1.setText("кг/с");
value_k2 = 1 / 3600;
break;
case MENU_main_G_5:
textView1.setText("м3/с");
value_k2 = 1 / 3600 * value_density;
break;
case MENU_main_G_6:
textView1.setText("фут3/нед");
break;
}
return super.onContextItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
double value_Q = 0;
double value_G = 0;
double value_tdiff = 0;
switch (v.getId()) {
case R.id.button1:
value_c = 4.187;
value_density = 1000;
Button1.setTextColor(Color.RED);
Button2.setTextColor(Color.GRAY);
Button6.setTextColor(Color.GRAY);
break;
case R.id.button2:
value_c = 1.005;
value_density = 1.2;
Button2.setTextColor(Color.RED);
Button1.setTextColor(Color.GRAY);
Button6.setTextColor(Color.GRAY);
break;
case R.id.button6:
value_c = 3.8;
value_density = 1113;
Button6.setTextColor(Color.RED);
Button1.setTextColor(Color.GRAY);
Button2.setTextColor(Color.GRAY);
break;
case R.id.button3:
Button3.setTextColor(Color.RED);
Button4.setTextColor(Color.GRAY);
Button5.setTextColor(Color.GRAY);
if (TextUtils.isEmpty(editText2.getText().toString())
|| TextUtils.isEmpty(editText3.getText().toString())) {
return;
}
Toast toast = Toast.makeText(MainActivity.this, "рассчитывается мощность", Toast.LENGTH_LONG);
toast.show();
value_G = Float.parseFloat(editText2.getText().toString());
value_tdiff = Float.parseFloat(editText3.getText().toString());
value_Q = value_G * value_tdiff * value_k2 * value_c / (3600 * value_k1);
editText1.setText(String.format( Locale.US,"%.3f", value_Q ) + " " );
break;
case R.id.button4:
Button3.setTextColor(Color.GRAY);
Button4.setTextColor(Color.RED);
Button5.setTextColor(Color.GRAY);
if (TextUtils.isEmpty(editText1.getText().toString())
|| TextUtils.isEmpty(editText3.getText().toString())) {
return;
}
Toast toast1 = Toast.makeText(MainActivity.this, "рассчитывается расход", Toast.LENGTH_LONG);
toast1.show();
value_Q = Float.parseFloat(editText1.getText().toString());
value_tdiff = Float.parseFloat(editText3.getText().toString());
value_G = value_Q * value_k1 * 3600 / (value_tdiff * value_c * value_k2);
editText2.setText(String.format( Locale.US,"%.3f", value_G ) + " " );
break;
case R.id.button5:
Button3.setTextColor(Color.GRAY);
Button4.setTextColor(Color.GRAY);
Button5.setTextColor(Color.RED);
if (TextUtils.isEmpty(editText1.getText().toString())
|| TextUtils.isEmpty(editText2.getText().toString())) {
return;
}
Toast toast2 = Toast.makeText(MainActivity.this, "рассчитывается разность температур", Toast.LENGTH_LONG);
toast2.show();
value_Q = Float.parseFloat(editText1.getText().toString());
value_G = Float.parseFloat(editText2.getText().toString());
value_tdiff = value_Q * value_k1 * 3600 / (value_G * value_k2 * value_c);
editText3.setText(String.format( Locale.US,"%.3f", value_tdiff ) + " " );
break;
case R.id.button7:
/*Intent intent1 = new Intent(this, MainActivity.class);
startActivity(intent);*/
break;
case R.id.button8:
Intent intent2 = new Intent(this, Main2Activity.class);
startActivity(intent2);
break;
case R.id.button9:
Intent intent3 = new Intent(this, Main3Activity.class);
startActivity(intent3);
break;
从这一点开始使用数据库
case R.id.button10:
DBHelper dbHelper = new DBHelper(this);
SQLiteDatabase db;
ContentValues cv = new ContentValues();
db = dbHelper.getWritableDatabase();
String temperature = editText5.getText().toString();
String percent = editText7.getText().toString();
if (temperature.equalsIgnoreCase("")) {
break;
}
if (percent.equalsIgnoreCase("")) {
break;
}
Log.d(LOG_TAG, "DB search");
Cursor c1 = db.query("mytable", null, "temperature , percent = ? , ?", new String[]{temperature, percent}, null, null, null);
if (c1.moveToFirst()) {
int ckoef = c1.getColumnIndex("ckoef");
int density = c1.getColumnIndex("density");
do {
textView9.setText(c1.getString(ckoef));
textView11.setText(c1.getString(density));
} while (c1.moveToNext());
} else
textView9.setText("oops");
textView11.setText("no matches");
c1.close();
dbHelper.close();
break;
}
}
class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "myDB", null, DB_VERSION);
}
public void onCreate(SQLiteDatabase db) {
int percent[] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50};
int temperature[] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
double ckoef [] = {4.025, 4.034, 4.043, 4.057, 4.075, 4.085, 4.094, 4.103, 4.113, 4.122, 4.132, 3.87, 3.885, 3.9, 3.915, 3.93, 3.945, 3.96, 3.975, 3.99, 4.005, 4.02,
3.966, 3.977, 3.988, 3.999, 4.010, 4.022, 4.034, 4.046, 4.058, 4.069, 4.081, 3.436, 3.46, 3.5, 3.53, 3.56, 3.595, 3.62, 3.65, 3.68, 3.715, 3.74, 3.21, 3.25, 3.28, 3.32, 3.36, 3.399, 3.433, 3.472, 3.511, 3.546, 3.850 };
double density [] = {1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1036, 1033, 1030, 1026, 1022, 1018, 1014, 1010, 1006, 1001.5, 997, 1027.852, 1025.148, 1018.85, 1015.23, 1011.63, 1008, 1004.37, 1000.741, 996.61, 992.482, 1072.667, 1068.333, 1064, 1058.167, 1052.167, 1046.667, 1041, 1035.333, 1029.667, 1023.5, 1017.333, 1089.852, 1084.926, 1080, 1073.57, 1067.148, 1061.185, 1055.222, 1049.259, 1043.296, 1036.833, 1030.37};
Log.d(LOG_TAG, "--- onCreate database ---");
// создаем таблицу с полями
db.execSQL("create table mytable ("
+ "id integer primary key autoincrement," + "percent integer,"
+ "temperature integer," + "ckoef double" + "density double" + ");");
// проверка существования записей
Cursor c = db.query("mytable", null, null, null, null, null, null);
ContentValues cv = new ContentValues();
// заполним таблицу
for (int i = 0; i < 55; i++) {
cv.put("percent", percent[i]);
cv.put("temperature", temperature[i]);
cv.put("ckoef", ckoef[i]);
cv.put("density", density[i]);
Log.d(LOG_TAG, "id = " + db.insert("mytable", null, cv));
}
db.close();
Log.d(LOG_TAG, "все данные");
c = db.query("mytable", null, null, null, null, null, null);
c.close();
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion == 1 && newVersion == 2) {
db.delete("mytable", null, null);
Log.d(LOG_TAG, "--- onUpgrade database ---");
ContentValues cv = new ContentValues();
// данные для таблицы должностей
int percent[] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50};
int temperature[] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
double ckoef [] = {4.025, 4.034, 4.043, 4.057, 4.075, 4.085, 4.094, 4.103, 4.113, 4.122, 4.132, 3.87, 3.885, 3.9, 3.915, 3.93, 3.945, 3.96, 3.975, 3.99, 4.005, 4.02,
3.966, 3.977, 3.988, 3.999, 4.010, 4.022, 4.034, 4.046, 4.058, 4.069, 4.081, 3.436, 3.46, 3.5, 3.53, 3.56, 3.595, 3.62, 3.65, 3.68, 3.715, 3.74, 3.21, 3.25, 3.28,
3.32, 3.36, 3.399, 3.433, 3.472, 3.511, 3.546, 3.850 };
double density [] = {1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1036, 1033, 1030, 1026, 1022, 1018, 1014, 1010, 1006, 1001.5,
997, 1027.852, 1025.148, 1018.85, 1015.23, 1011.63, 1008,
1004.37, 1000.741, 996.61, 992.482, 1072.667, 1068.333, 1064, 1058.167, 1052.167, 1046.667, 1041, 1035.333, 1029.667, 1023.5, 1017.333, 1089.852, 1084.926, 1080,
1073.57, 1067.148, 1061.185, 1055.222, 1049.259, 1043.296, 1036.833, 1030.37};
db.beginTransaction();
try {
// создаем таблицу должностей
db.execSQL("create table mytable ("
+ "id integer primary key autoincrement," + "percent integer,"
+ "temperature integer," + "ckoef double" + "density double" + ");");
// заполняем ее
for (int i = 0; i < 55; i++) {
cv.put("percent", percent[i]);
cv.put("temperature", temperature[i]);
cv.put("ckoef", ckoef[i]);
cv.put("density", density[i]);
Log.d(LOG_TAG, "id = " + db.insert("mytable", null, cv));
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
}
}
}
错误日志:
11-17 06:48:17.345 1024-1024/com.example.prog E/SQLiteLog: (1) table mytable has no column named density
11-17 06:48:17.371 1024-1024/com.example.prog E/SQLiteDatabase: Error inserting temperature=0 ckoef=4.025 density=1012.5 percent=10
11-17 06:48:17.371 1024-1024/com.example.prog E/SQLiteDatabase: android.database.sqlite.SQLiteException: table mytable has no column named density (code 1): , while compiling: INSERT INTO mytable(temperature,ckoef,density,percent) VALUES (?,?,?,?)
11-17 06:48:17.371 1024-1024/com.example.prog E/SQLiteDatabase: at
请帮助,注意button10和以下代码
答案 0 :(得分:0)
忘记在create table statement中的两列(ckoef and density)
之间添加“,”。
db.execSQL("create table mytable ("
+ "id integer primary key autoincrement," + "percent integer,"
+ "temperature integer," + "ckoef double," + "density double" + ");");