Eclipse android数据库:如何在插入“注释”后插入日期

时间:2012-12-09 01:37:55

标签: android eclipse

我正在尝试编写一个Race数据库,您可以在其中输入比赛名称和比赛日期,然后单击该比赛名称,然后编辑/删除它或日期。我输入日期时遇到问题。下面是我的LogCat错误。 我究竟做错了什么 ?

//LOGCAT
12-08 20:15:35.710: I/Database(1648): sqlite returned: error code = 1, msg = near "FROM": syntax error
12-08 20:15:35.710: E/ERROR(1648): ERROR IN CODE: android.database.sqlite.SQLiteException: near "FROM": syntax error: , while compiling: SELECT _id, note FROM Races, date FROM Races
12-08 20:15:35.721: W/System.err(1648): android.database.sqlite.SQLiteException: near "FROM": syntax error: , while compiling: SELECT _id, note FROM Races, date FROM Races
12-08 20:15:35.730: W/System.err(1648):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)

//View races java code
package com.CIS2818.tritracker;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class View_races extends Activity {


NoteAdapter adapter=null;
RaceHelper helper2=null;
Cursor dataset_cursor=null;
EditText editNote2=null;
EditText editNote3=null;
String noteId2=null;

String TAG = "INFORMATION";

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try
    {
        setContentView(R.layout.activity_view_races);

        ListView list2=(ListView)findViewById(R.id.list2);
        editNote2 = (EditText)findViewById(R.id.myEditText2);
        editNote3 = (EditText)findViewById(R.id.myEditText3);

        helper2=new RaceHelper(this);

        dataset_cursor=helper2.getAll();

        startManagingCursor(dataset_cursor);

        adapter=new NoteAdapter(dataset_cursor);

        list2.setAdapter(adapter);


        Button btnSimple2 = (Button) findViewById(R.id.btnSimple2);
        btnSimple2.setOnClickListener(onSave);
        Button btnDelete2 = (Button) findViewById(R.id.btnDelete2);
        btnDelete2.setOnClickListener(onDelete);
        list2.setOnItemClickListener(onListClick);


    }
    catch (Exception e)
    {

        Log.e("ERROR", "ERROR IN CODE: " + e.toString());
        e.printStackTrace();
    } 
}
@Override
public void onDestroy() {
    super.onDestroy();
    helper2.close();
}

private View.OnClickListener onSave=new View.OnClickListener() {
    @SuppressWarnings("deprecation")
    public void onClick(View v) {
        Log.i(TAG,"You passed through the save method");
        if (noteId2==null) {
            helper2.insert(editNote2.getText().toString(),editNote3.getText().toString());
        }
        else{
            helper2.update(noteId2, editNote2.getText().toString(),editNote3.getText().toString());

            noteId2=null;

        }
        dataset_cursor.requery();

        editNote2.setText("");
    }
};    


private View.OnClickListener onDelete=new View.OnClickListener() {
    @SuppressWarnings("deprecation")
    public void onClick(View v) {

        if (noteId2==null) {
            return;
        }
        else{

            helper2.delete(noteId2);

            noteId2=null;

        }
        dataset_cursor.requery();

        editNote2.setText("");
    }
};    

private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent,
                            View view, int position,
                            long id2) 
    {

        noteId2 =String.valueOf(id2);

        Cursor c=helper2.getById(noteId2);


        c.moveToFirst();

        editNote2.setText(helper2.getNote(c));

    }
};

class NoteAdapter extends CursorAdapter {
    @SuppressWarnings("deprecation")
    NoteAdapter(Cursor c) {
        super(View_races.this, c);
    }

    @Override
    public void bindView(View row, Context ctxt,Cursor c) {
        NoteHolder2 holder=(NoteHolder2)row.getTag();

        holder.populateFrom(c, helper2);
    }

    @Override
    public View newView(Context ctxt, Cursor c,ViewGroup parent) {
        LayoutInflater inflater=getLayoutInflater();
        View row=inflater.inflate(R.layout.row2, parent, false);
        NoteHolder2 holder=new NoteHolder2(row);

        row.setTag(holder);

        return(row);
    }
}

static class NoteHolder2 {
    private TextView noteText2=null;

    NoteHolder2(View row) {
        noteText2=(TextView)row.findViewById(R.id.note2);
    }

    void populateFrom(Cursor c, RaceHelper helper) {
        noteText2.setText(helper.getNote(c));

    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_view_races, menu);
    return true;} 
  //Button Method to return to the main Menu
 public void Menu(View v){
    Intent intent = new Intent(this, MainMenuActivity.class);
    startActivity(intent);
 }
 //Button Method to go to the Race Activity
 public void Races(View v){
    Intent intent = new Intent(this, UpComingRaceActivity.class);
    startActivity(intent);
}
}

// RaceHelper java code
package com.CIS2818.tritracker;
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;



class RaceHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="Races.db";
private static final int SCHEMA_VERSION=2;
String TAG = "INFORMATION";
public RaceHelper(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE Races (_id INTEGER PRIMARY KEY AUTOINCREMENT, note TEXT, date TEXT);");
    Log.i(TAG, "You are in the creation of db");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
public void insert(String note, String date) {

    ContentValues cv=new ContentValues();
    cv.put("note", note);
    getWritableDatabase().insert("Races", "note", cv);
    Log.i(TAG, "You have succesfully inserted into the db");
}
public void update(String id, String note, String date) {
    ContentValues cv=new ContentValues();
    String[] args={id};

    cv.put("note", note);

    getWritableDatabase().update("Races", cv, "_id=?", args);

}
public void delete(String id) {

    getWritableDatabase().delete("Races", "_id=?", new String[] {id});
}



public Cursor getAll() {
    return(getReadableDatabase()
                    .rawQuery("SELECT _id, note FROM Races, date FROM Races",
                                        null));
}


public String getNote(Cursor c) {
    return(c.getString(1));
}

public Cursor getById(String id) {
    String[] args={id};

    return(getReadableDatabase()
                    .rawQuery("SELECT _id, note FROM Races WHERE _id=?",
                                        args));
}

1 个答案:

答案 0 :(得分:1)

更改以下行

getReadableDatabase().rawQuery("SELECT _id, note FROM Races, date FROM Races",
                                        null)

getReadableDatabase().rawQuery("SELECT _id, note, data FROM Races",
                                        null)