使用Android Studio创建和插入SQLite数据库时出错。应用程序在启动时崩溃

时间:2015-08-13 09:16:11

标签: android database android-studio android-sqlite

我是Android的新手,在java语言方面也不是那么彻底。所以请提供这个问题的解决方案。我想在MainActivity中显示注册用户的名称,注册活动用于从用户那里获取详细信息。我想学习如何使用DBhelper创建数据库。任何有关这方面的信息都将非常感激。如果任何人都可以提供有关如何创建数据库以及如何插入和删除数据库的详细信息,那将会有很大帮助。提前致谢

MainActivity.java

Context context;
DBHelper dbHelper;
ListView listView;
ArrayList nameList;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.listView);
    ArrayList<String> nameList = new ArrayList<String>();
    dbHelper = new DBHelper(this);
    ArrayList<StoreDetails> arrayList = dbHelper.getAllContacts();
    for (StoreDetails sd : arrayList) {
        nameList.add(sd.getName());
    }
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, nameList);
    listView.setAdapter(arrayAdapter);

}

@Override
protected void onResume() {
    super.onResume();

    Button addContact = (Button) findViewById(R.id.button);
    addContact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent registerIntent = new Intent(MainActivity.this, RegisterDetails.class);
            startActivity(registerIntent);
        }
    });

}

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

}

DBhelper.java

static final String DATABASE_NAME = "MySqldatabase.db";
static final String CONTACT_TABLE_NAME = "Contacts";
static final String CONTACT_COLUMN_ID = "Id";
static final String CONTACT_COLUMN_NAME = "Name";
static final String CONTACT_COLUMN_PHONE = "Phone";
StoreDetails sd = new StoreDetails();
Cursor res;

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

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table Contacts" + "(Id integer primary key,Name text,Phone text)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS Contacts");
    onCreate(db);
}

public void insertContact(StoreDetails storeDetails) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(CONTACT_COLUMN_NAME, sd.getName());
    contentValues.put(CONTACT_COLUMN_PHONE, sd.getPhone_number());
    db.insert(CONTACT_TABLE_NAME, null, contentValues);
    db.close();

}

public Cursor getData(int id) {
    SQLiteDatabase db = this.getReadableDatabase();
    res = db.rawQuery("Select * from Contacts where Id=" + id + "", null);
    return res;

}

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

public ArrayList<StoreDetails> getAllContacts() {

    ArrayList<StoreDetails> arrayList = new ArrayList<StoreDetails>();
    SQLiteDatabase db = this.getReadableDatabase();

    res = db.rawQuery("Select * from Contacts", null);
    if (res.moveToFirst()) {
        do {
            StoreDetails contact = new StoreDetails();
            contact.setId(res.getString(res.getColumnIndex(CONTACT_COLUMN_ID)));
            contact.setName(res.getString(res.getColumnIndex(CONTACT_COLUMN_NAME)));
            contact.setPhone_number(res.getString(res.getColumnIndex(CONTACT_COLUMN_PHONE)));
            arrayList.add(contact);
        }
        while (res.moveToNext());
    }return arrayList;
}

}

RegisterDetails.java

Button submit;
EditText user_name, phone_number;
StoreDetails sd = new StoreDetails();
DBHelper dbHelper=new DBHelper(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.registerdetails);

    submit = (Button) findViewById(R.id.button2);
    user_name = (EditText) findViewById(R.id.editText);
    phone_number = (EditText) findViewById(R.id.editText2);
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sd.setName(user_name.getText().toString());
            sd.setPhone_number(phone_number.getText().toString());
            dbHelper.insertContact(sd);
            Toast.makeText(getApplicationContext(),"Contact added successfully",Toast.LENGTH_LONG).show();
            Intent registeredIntent=new Intent(RegisterDetails.this,MainActivity.class);
            startActivity(registeredIntent);
        }
    });
}

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

}

StoreDetails.java

String name, phone_number, id;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPhone_number() {
    return phone_number;
}

public void setPhone_number(String phone_number) {
    this.phone_number = phone_number;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

}

1 个答案:

答案 0 :(得分:1)

您可以查看以下链接,了解有关Android中SQLite的更多详细信息。

  

http://www.vogella.com/tutorials/AndroidSQLite/article.html

     

http://hmkcode.com/android-simple-sqlite-database-tutorial/

根据我的个人经验,需要更好地处理onUpgrade()方法。目前,对于任何数据库升级,它将删除联系人表并创建一个新联系人表。当用户已经使用您以前的应用程序版本时,这不是一个好主意。

对于您收到的错误,您是否可以共享错误日志以获得更详细的响应。