数据库已创建,但无法添加任何内容

时间:2013-09-24 12:05:25

标签: android database insert

嘿,我已经创建了数据库。我正在添加一个通过插入查询但没有添加到数据库中 这是我的DBManager.java代码

        package com.database;

    import java.util.ArrayList;

    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.util.Log;

public class DBManager {

    public SQLiteDatabase mDatabase;
    public MyDBHandler mDBhandler;

    private String[] allEvents = { MyDBHandler.COLUMN_ID,
            MyDBHandler.COLUMN_ICON, MyDBHandler.COLUMN_EVENTNAME };

    public static final String TAG = "MyActivityTag";

    public DBManager(Context context) {
        mDBhandler = new MyDBHandler(context);
        Log.d(TAG, "db manager created");
    }

    public void open() throws SQLException {
        mDatabase = mDBhandler.getWritableDatabase();
    }

    public void close() throws SQLException {
        mDBhandler.close();
    }

    public void addEvent(Event event) {
        Log.d(TAG, "addEvent method in eventmanager with icon and name :"
                + event.getIcon() + event.getEventName());

        String insertQuery = " INSERT INTO " + MyDBHandler.TABLE_EVENTS + "("
                + MyDBHandler.COLUMN_ICON + "," + MyDBHandler.COLUMN_EVENTNAME
                + ")" + " VALUES ( ' " + event.getIcon() + " ' , '"
                + event.getEventName() + "  ' )";

        mDatabase.execSQL(insertQuery);
    }

    public void deleteEvent(String eventname) {

        Log.d(TAG, "in deleteEvent method in DBManager");

        String deleteQuery = "Select * FROM " + MyDBHandler.TABLE_EVENTS
                + " WHERE " + MyDBHandler.COLUMN_EVENTNAME + " =  \""
                + eventname + "\"";

        mDatabase = mDBhandler.getWritableDatabase();

        Cursor cursor = mDatabase.rawQuery(deleteQuery, null);
        Event event = new Event();
        if (cursor.moveToFirst()) {
            event.setID(Integer.parseInt(cursor.getString(0)));
            mDatabase.delete(MyDBHandler.TABLE_EVENTS, MyDBHandler.COLUMN_ID
                    + " = ?", new String[] { String.valueOf(event.getID()) });
            cursor.close();
        }
        Log.d(TAG, "Event deleted and database closed");
        mDatabase.close();
    }

    public ArrayList<Event> getallEvents() {
        Log.d(TAG, "getallEvents in DBmanager");
        ArrayList<Event> events = new ArrayList<Event>();

        Cursor cursor = mDatabase.query(MyDBHandler.TABLE_EVENTS, allEvents,
                null, null, null, null, null);

        if (cursor.moveToFirst()) {

            do {
                Event event = cursorToEvent(cursor);
                events.add(event);
            } while (cursor.moveToNext());

        }
        cursor.close();
        return events;
    }

    private Event cursorToEvent(Cursor cursor) {
        Event event = new Event();
        event.setID(cursor.getInt(0));
        event.setEventName(cursor.getString(1));
        return event;
    }
}

我没有收到任何错误日志,但是当我在数据库中检查时,没有添加任何内容。

我的MainActivity.java

package com.database;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.ajay.database.R;

public class MainActivity extends Activity {

    private DBManager mDbManager;
    private EditText mName;
    public CustomListAdapter mAdapter;
    public ArrayList<Event> mEvents;
    public ListView mListview;

    public String TAG = "MyActivityTag";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("Attendance Tracker");

        mDbManager = new DBManager(this);
        mDbManager.open();
        Log.d(TAG, "Dbmanager.open called");

        mEvents = mDbManager.getallEvents();

        mAdapter = new CustomListAdapter(MainActivity.this, mEvents);

        mListview = (ListView) findViewById(R.id.listview);
        mListview.setAdapter(mAdapter);

    }

    public void addClick(View view) {

        Log.d(TAG, "Add button clicked");
        Event event = new Event();
        mName = (EditText) findViewById(R.id.eventNameText);
        event.mEventName = mName.getText().toString();
        event.setIcon("icon");
        mDbManager.addEvent(event);
        mAdapter.add(event);
        mAdapter.notifyDataSetChanged();
        mName.setText(null);

    }

    @Override
    protected void onResume() {
        mDbManager.open();
        super.onResume();
    }

    @Override
    protected void onPause() {
        mDbManager.close();
        super.onPause();
    }
}

和customAdapter.java

 package com.database;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.ajay.database.R;

public class CustomListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Event> mEventList;
    public DBManager mDBManager;
    public static final String TAG = "MyActivityTag";

    public CustomListAdapter(Context context, ArrayList<Event> list) {
        Log.d(TAG, "Custom Adapter constructor is called");
        this.context = context;
        mEventList = list;
        mDBManager = new DBManager(context);
    }

    @Override
    public int getCount() {
        return mEventList.size();
    }

    @Override
    public Object getItem(int position) {
        return mEventList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void add(Event event) {

        mEventList.add(event);
        notifyDataSetChanged();
        Log.d(TAG, "Event Added");

    }

    public void remove(Event event) {

        mEventList.remove(event);
        notifyDataSetChanged();
        Log.d(TAG, "Event Deleted");

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final Event event = mEventList.get(position);

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.event_list_row, null);
        }

        TextView mEventName = (TextView) convertView
                .findViewById(R.id.eventName);
        mEventName.setText(event.getEventName());

        TextView mIcon = (TextView) convertView.findViewById(R.id.iconName);
        mIcon.setText(event.getIcon());

        final ImageButton mDeleteButton = (ImageButton) convertView
                .findViewById(R.id.deleteButton);
        mDeleteButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d(TAG, "Delete Button clicked");
                Log.d(TAG, "Event want to delete is " + event.getEventName());
                mDBManager.deleteEvent(event.getEventName());
                remove(event);
                Log.d(TAG, "Database refreshed");
                notifyDataSetChanged();

            }
        });

        return convertView;
    }
}

和MyDBHandler.java

package com.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MyDBHandler extends SQLiteOpenHelper {

    protected static final int DATABASE_VERSION = 1;
    protected static final String DATABSE_NAME = "eventDB.db";
    protected static final String TABLE_EVENTS = "Event";

    public static final String COLUMN_ID = "id";
    public static final String COLUMN_ICON = "icon";
    public static final String COLUMN_EVENTNAME = "eventname";

    public static final String TAG = "MyActivityTag";

    public MyDBHandler(Context context) {
        super(context, DATABSE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_EVENTS_TABLE = " CREATE TABLE " + TABLE_EVENTS + "("
                + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_ICON + " TEXT,"
                + COLUMN_EVENTNAME + " TEXT" + ")";
        db.execSQL(CREATE_EVENTS_TABLE);
        Log.d(TAG, "Table Created in database");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_EVENTS);
        onCreate(db);
        Log.d(TAG, "database updated");
    }

}

我不想使用CustomValues我只想通过execSQL()添加数据

4 个答案:

答案 0 :(得分:0)

在插入方法addEvent调用结束时

mDatabase.close();

修改

还请尝试插入类似的值:

ContentValues values = new ContentValues();
values.put(MyDBHandler.COLUMN_ICON, event.getIcon());
values.put(MyDBHandler.COLUMN_EVENTNAME, event.getEventName());

mDatabase.insert(MyDBHandler.TABLE_EVENTS, null, values);

答案 1 :(得分:0)

试试这个,你没有使用交易。

mDatabase.beginTransaction();
mDatabase.execSQL(insertQuery);
mDatabase.setTransactionSuccessful();
mDatabase.endTransaction();

答案 2 :(得分:0)

嗨,在那种方法中,更好地尝试这可能会有效。

public void addEvent(Event event) {
    SQLiteDatabase writableDatabase = getWritableDatabase();
    ContentValues values=new ContentValues();
    values.put(MyDBHandler.COLUMN_ICON, event.getIcon());
    values.put(MyDBHandler.COLUMN_EVENTNAME, event.getEventName());
    writableDatabase.insert(MyDBHandler.TABLE_EVENTS, null, values);
}

尝试这个经过修改的东西。

答案 3 :(得分:0)

private Event cursorToEvent(Cursor cursor) {
        Event event = new Event();
        event.setID(cursor.getInt(0));
        event.setIcon(cursor.getString(1));
        Log.d(TAG, "Icon name coming out" + cursor.getString(1));
        event.setEventName(cursor.getString(2));
        Log.d(TAG, "Event name coming out " + cursor.getString(2));
        return event;
    }

这是我的答案的解决方案。 一切都很好,期待这一点。