私のプログラムはうまくいきましたが、このプロジェクトでは、変数名はデータベース固有のものであり、私自身の変数名を使うのは習慣が悪いです。私は、テーブルを作成しようとするとテーブル(SQLite)で何が問題になっていますか?
さて、それは問題があることを私に見せている(変更を行った後*値
を選択し、プログラムが私にこのログを示す、クラッシュしたに過ぎずコンパイルします。私は6費やしてきました私のコードと私のテーブルで何が間違っているのか調べようとしています。私のプログラムの前のバージョンをチェックして再チェックしてください。 、 "VALUE_COLUMNS_ID"は使用されませんが、idパラメータには何も触れていません。
私は何も表示されません私のテーブルに構文エラーがあります。助けてください。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.carlos.application1, PID: 2565
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.carlos.application1/com.example.carlos.application1.MainActivity}: android.database.sqlite.SQLiteException: near "values": syntax error (code 1): , while compiling: select * from values
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.database.sqlite.SQLiteException: near "values": syntax error (code 1): , while compiling: select * from values
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
at com.example.carlos.application1.DBHelper.getAllCotacts(DBHelper.java:99)
at com.example.carlos.application1.MainActivity.onCreate(MainActivity.java:50)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
私は推測したくない、またはそれ以上の変更を加えることはありません。これは私のプロジェクトの最終段階です。
DBヘルパー:
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBName.db";
public static final String VALUES_TABLE_NAME = "values";
public static final String VALUES_COLUMN_ID = "id";
public static final String VALUES_COLUMN_CONDUCTIVITY = "conductivity";
public static final String VALUES_COLUMN_MOISTURE = "moisture";
public static final String VALUES_COLUMN_OXYGEN = "oxygen";
public static final String VALUES_COLUMN_PH = "ph";
private HashMap hp;
public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table values " +
"(id integer primary key, conductivity text,ph text,oxygen text, moisture text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS values");
onCreate(db);
}
public boolean insertContact (String conductivity, String ph, String oxygen, String moisture)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("conductivity", conductivity);
contentValues.put("ph", ph);
contentValues.put("oxygen", oxygen);
contentValues.put("moisture", moisture);
db.insert("values", null, contentValues);
return true;
}
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from values where id="+id+"", null);
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, VALUES_TABLE_NAME);
return numRows;
}
public boolean updateContact (Integer id, String conductivity, String ph, String oxygen, String moisture)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("conductivity", conductivity);
contentValues.put("ph", ph);
contentValues.put("oxygen", oxygen);
contentValues.put("moisture", moisture);
db.update("values", contentValues, "id = ? ", new String[] { Integer.toString(id) });
return true;
}
public Integer deleteContact (Integer id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("values",
"id = ? ",
new String[] { Integer.toString(id) });
}
public ArrayList<String> getAllCotacts()
{
ArrayList<String> array_list = new ArrayList<String>();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from values", null);
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(VALUES_COLUMN_CONDUCTIVITY)));
res.moveToNext();
}
return array_list;
}
}
主な活動:
package com.example.carlos.application1;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.widget.Toast;
import android.widget.Button;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.DialogInterface;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "MESSAGE";
private ListView obj;
DBHelper mydb;
int status = 0;
Button hp_1, HP2, fo, previous, next, MZ, Control, show, home;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button home = (Button) findViewById(R.id.home);
registerForContextMenu(home);
mydb = new DBHelper(this);
ArrayList array_list = mydb.getAllCotacts();
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, array_list);
Button hp_1 = (Button) findViewById(R.id.hp_1);
obj = (ListView) findViewById(R.id.listView1);
obj.setAdapter(arrayAdapter);
obj.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(), DisplayValues.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
hp_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
status = 1;
Bundle bundle = new Bundle();
bundle.putInt("status", status);
Intent intent = new Intent(MainActivity.this, DisplayValues.class);
intent.putExtras(bundle);
startActivity(intent);/*Intent intent = new Intent(first.this, second.class);
Bundle bundle = new Bundle();
bundle.putInt("index", index);
intent.putExtras(bundle);
startActivity(intent); */
}
});
}
@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){
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.item1:Bundle dataBundle = new Bundle();
dataBundle.putInt("id", 0);
Intent intent = new Intent(getApplicationContext(),DisplayValues.class);
intent.putExtras(dataBundle);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public boolean onKeyDown(int keycode, KeyEvent event) {
if (keycode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
}
return super.onKeyDown(keycode, event);
}
//dfsdfasdfasdfasd
}
そして、私は私のDBにエントリを取得し表示値クラス、:
package com.example.carlos.application1;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class DisplayValues extends Activity {
int from_Where_I_Am_Coming = 0;
private DBHelper mydb ;
TextView conductivity ;
TextView ph;
TextView moisture;
TextView oxygen;
int id_To_Update = 0;
public void backButtonHandler() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
DisplayValues.this);
// Setting Dialog Title
alertDialog.setTitle("Leave the page?");
// Setting Dialog Message
alertDialog.setMessage("Are you sure you want to leave without saving the Entries?");
// Setting Icon to Dialog
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onBackPressed() {
backButtonHandler();
return;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
conductivity = (TextView) findViewById(R.id.editTextConductivity);
oxygen= (TextView) findViewById(R.id.editTextOxygen);
moisture = (TextView) findViewById(R.id.editTextMoisture);
ph = (TextView) findViewById(R.id.editTextpH);
mydb = new DBHelper(this);
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
int Value = extras.getInt("id");
if(Value>0){
//means this is the view part not the add contact part.
Cursor rs = mydb.getData(Value);
id_To_Update = Value;
rs.moveToFirst();
String condc = rs.getString(rs.getColumnIndex(DBHelper.VALUES_COLUMN_CONDUCTIVITY));
String mois = rs.getString(rs.getColumnIndex(DBHelper.VALUES_COLUMN_MOISTURE));
String oxy = rs.getString(rs.getColumnIndex(DBHelper.VALUES_COLUMN_OXYGEN));
String phe = rs.getString(rs.getColumnIndex(DBHelper.VALUES_COLUMN_PH));
/* public static final String DATABASE_NAME = "MyDBName.db";
public static final String VALUES_TABLE_NAME = "values";
public static final String VALUES_COLUMN_CONDUCTIVITY = "conductivity";
public static final String VALUES_COLUMN_MOISTURE = "moisture";
public static final String VALUES_COLUMN_OXYGEN = "oxygen";
public static final String VALUES_COLUMN_PH = "ph";*/
if (!rs.isClosed())
{
rs.close();
}
Button b = (Button)findViewById(R.id.button1);
b.setVisibility(View.INVISIBLE);
conductivity.setText((CharSequence)condc);
conductivity.setFocusable(false);
conductivity.setClickable(false);
moisture.setText((CharSequence)mois);
moisture.setFocusable(false);
moisture.setClickable(false);
ph.setText((CharSequence)phe);
ph.setFocusable(false);
ph.setClickable(false);
oxygen.setText((CharSequence)oxy);
oxygen.setFocusable(false);
oxygen.setClickable(false);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
int Value = extras.getInt("id");
if(Value>0){
getMenuInflater().inflate(R.menu.display_contact, menu);
}
else{
getMenuInflater().inflate(R.menu.menu_main, menu);
}
}
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.Edit_Contact:
Button b = (Button)findViewById(R.id.button1);
b.setVisibility(View.VISIBLE);
conductivity.setEnabled(true);
conductivity.setFocusableInTouchMode(true);
conductivity.setClickable(true);
ph.setEnabled(true);
ph.setFocusableInTouchMode(true);
ph.setClickable(true);
moisture.setEnabled(true);
moisture.setFocusableInTouchMode(true);
moisture.setClickable(true);
oxygen.setEnabled(true);
oxygen.setFocusableInTouchMode(true);
oxygen.setClickable(true);
return true;
case R.id.Delete_Contact:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deleteContact)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteContact(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void run(View view)
{
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
int Value = extras.getInt("id");
if(Value>0){
if(mydb.updateContact(id_To_Update,conductivity.getText().toString(), ph.getText().toString(), oxygen.getText().toString(), moisture.getText().toString())){
Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
else{
Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show();
}
}
else{
if(mydb.insertContact(conductivity.getText().toString(), ph.getText().toString(), oxygen.getText().toString(), moisture.getText().toString())){
Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
}
}
}
に 'values'はSQLiteの[キーワード]である(http://www.sqlite.org/sessions/lang_keywords.html)。テーブル名として使用することはできません。 –