Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
714 views
in Technique[技术] by (71.8m points)

java - SQLiteException: unknown database

I made a custom Adapter for my ListView following this tutorial.
But when I run my app on my device, it gives an error when it's starting.
The error appears when the onCreate() method of the MainActivity tries to call the getData() method of the NotesDbHelper class.

Can you help me?

MainActivity.java

public class MainActivity extends Activity
{

private EditText mEditText;
private Button mButton;

NotesCustomAdapter notesCustomAdapter = null;
ListView listView = null;
NotesDbHelper database = null;
ArrayList<Notes> notes = null;

/** Called when the activity is first created.
 * @param savedInstanceState */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mButton = (Button) findViewById(R.id.button);
    mEditText = (EditText) findViewById(R.id.editText);

    database = new NotesDbHelper(this);
    notes = database.getData();
    notesCustomAdapter= new NotesCustomAdapter(this,R.layout.notes_details,notes);

    listView = (ListView) findViewById(R.id.simpleListView);
    listView.setAdapter(notesCustomAdapter);

    mButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String input = mEditText.getText().toString();

            if (input.length() > 0) {
            database.insertNote(input);
            }
        }
    });

    listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> a, View v, final int position, long id) {
    AlertDialog.Builder adb=new AlertDialog.Builder(MainActivity.this);
    adb.setTitle("Delete?");
    adb.setMessage("Are you sure you want to delete this note?");
    final int positionToRemove = position;
    adb.setNegativeButton("Cancel", null);
    adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            database.deleteNote(which);
            notes.remove(positionToRemove);
            notesCustomAdapter.remove(String.valueOf(positionToRemove));
            notesCustomAdapter.notifyDataSetChanged();
        }});
    adb.show();
    }
});
}

@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);
  }
}

NotesDbHelper.java

public class NotesDbHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "Notes.db";
    public static final String NOTES_TABLE_NAME = "Notes.user";
    public static final String NOTES_COLUMN_ID = "id";
    public static final String NOTES_COLUMN_NAME = "n_text";

    public NotesDbHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table  " + NOTES_TABLE_NAME +
                   "(_id integer primary key AUTOINCREMENT NOT NULL," + NOTES_COLUMN_NAME +
                   ")"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS "+ DATABASE_NAME);
        onCreate(db);
    }

    public boolean insertNote(String text) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("n_text", text);

        db.insert(NOTES_TABLE_NAME, null, contentValues);
        return true;
    }

    public ArrayList<Notes> getData() {
        SQLiteDatabase db = this.getReadableDatabase();
        ArrayList<Notes> notes = new ArrayList<Notes>();
        Cursor result = db.rawQuery("select * from "+ NOTES_TABLE_NAME , null);
        while(result.moveToNext()){
           notes.add( new Notes(result.getString(result.getColumnIndex(NOTES_COLUMN_NAME))));

        }
        return notes;
    }

    public boolean updateNotes(int id, int text) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        contentValues.put("n_text", text);

        db.update(NOTES_TABLE_NAME, contentValues, "id = ? ", new String[]{Integer.toString(id)});
        return true;
    }

    public Integer deleteNote(Integer id) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(NOTES_TABLE_NAME,
                "id = ? ",
                new String[]{Integer.toString(id)});
    }

}

Notes.java

    public class Notes {
    String text;

    public Notes(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

NotesCustomAdapter.java

public class NotesCustomAdapter extends ArrayAdapter{
    private Context context;
    private ArrayList<Notes> notes;

    public NotesCustomAdapter(Context context, int textViewResourceId, ArrayList objects) {
        super(context,textViewResourceId, objects);

        this.context= context;
        notes=objects;

    }

    private class ViewHolder
    {
        TextView text;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder=null;
        if (convertView == null)
        {
            LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.notes_details, null);

            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.text);
            convertView.setTag(holder);

        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        Notes textNotes = notes.get(position);
        holder.text.setText(textNotes.getText());
        return convertView;
    }
}

LogCat

the first line says:

java.lang.RuntimeException: Unable to start activity ComponentInfo{agenda.com/agenda.com.MainActivity}: android.database.sqlite.SQLiteException: unknown database Notes (code 1): while compiling: create table Notes.user(_id integer primary key AUTOINCREMENT NOT NULL,n_text)

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why Notes.user? You're putting an unnecessary dot. Go compare to the link you've referenced.

Just use Notes or UserNotes


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...