The app does not working after add android database insert code
up vote
0
down vote
favorite
I am making a simple app. The app get data through Edittext and insert it in database and output data from database to textView.
public class MainActivity extends AppCompatActivity {
private TextView status;
public static final String TAG = "MainActivity";
private static String DATABASE_NAME = "20180720";
private static String TABLE_NAME = "dayList";
private static int DATABASE_VERSION = 1;
private DatabaseHelper dbHelper;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (TextView) findViewById(R.id.status);
final EditText input01 = (EditText) findViewById(R.id.input01);
final EditText input02 = (EditText) findViewById(R.id.input02);
final EditText input03 = (EditText) findViewById(R.id.input03);
Button queryBtn = (Button) findViewById(R.id.queryBtn);
queryBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
boolean isOpen = openDatabase();
if (isOpen) {
executeRawQuery();
executeRawQueryParam2();
}
}
});
Button addBtn = (Button) findViewById(R.id.addBtn);
addBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name = input01.getText().toString();
int age = Integer.parseInt(input02.getText().toString());
String phone = input03.getText().toString();
String sqlInsert = "insert into dayList (name, age, phone) values (" +
"'" + name + "', " + age + " ,'" + phone + "');";
db.execSQL(sqlInsert);
}
});
}
private boolean openDatabase() {
println("opening database [" + DATABASE_NAME + "].");
dbHelper = new DatabaseHelper(this);
db = dbHelper.getWritableDatabase();
return true;
}
private void executeRawQuery() {
println("nexecuteRawQuery called.n");
Cursor c1 = db.rawQuery("select count(*) as Total from " + TABLE_NAME, null);
println("cursor count : " + c1.getCount());
c1.moveToNext();
println("record count : " + c1.getInt(0));
c1.close();
}
private void executeRawQueryParam() {
println("nexecuteRawQueryParam called.n");
String SQL = "select name, age, phone "
+ " from " + TABLE_NAME
+ " where age > ?";
String args= {"30"};
Cursor c1 = db.rawQuery(SQL, args);
int recordCount = c1.getCount();
println("cursor count : " + recordCount + "n");
for (int i = 0; i < recordCount; i++) {
c1.moveToNext();
String name = c1.getString(0);
int age = c1.getInt(1);
String phone = c1.getString(2);
println("Record #" + i + " : " + name + ", " + age + ", " + phone);
}
c1.close();
}
private void executeRawQueryParam2() {
println("nexecuteRawQueryParam2 called.n");
String columns = {"name", "age", "phone"};
Cursor c1 = db.query(TABLE_NAME, columns,
null, null,
null, null, null);
int recordCount = c1.getCount();
println("cursor count : " + recordCount + "n");
for (int i = 0; i < recordCount; i++) {
c1.moveToNext();
String name = c1.getString(0);
int age = c1.getInt(1);
String phone = c1.getString(2);
println("Record #" + i + " : " + name + ", " + age + ", " + phone);
}
c1.close();
}
private void println(String msg) {
Log.d(TAG, msg);
status.append("n" + msg);
}
private class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
println("creating table [" + TABLE_NAME + "].");
try {
String DROP_SQL = "drop table if exists " + TABLE_NAME;
db.execSQL(DROP_SQL);
} catch(Exception ex) {
Log.e(TAG, "Exception in DROP_SQL", ex);
}
String CREATE_SQL = "create table " + TABLE_NAME + "("
+ " _id integer PRIMARY KEY autoincrement, "
+ " name text, "
+ " age integer, "
+ " phone text)";
try {
db.execSQL(CREATE_SQL);
} catch(Exception ex) {
Log.e(TAG, "Exception in CREATE_SQL", ex);
}
println("inserting records.");
try {
db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('John', 20, '010-7788-1234');" );
db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('Mike', 35, '010-8888-1111');" );
db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('Sean', 26, '010-6677-4321');" );
} catch(Exception ex) {
Log.e(TAG, "Exception in insert SQL", ex);
}
}
public void onOpen(SQLiteDatabase db) {
println("opened database [" + DATABASE_NAME + "].");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ".");
}
}
}
When I push the queryBtn, the app is working normally, outputting information (which already saved in database) to the textView.
but when I push addBtn button, the app has stopped.
what is the problem?
And how can I insert the information input by Edittiext to the database?
android-edittext
add a comment |
up vote
0
down vote
favorite
I am making a simple app. The app get data through Edittext and insert it in database and output data from database to textView.
public class MainActivity extends AppCompatActivity {
private TextView status;
public static final String TAG = "MainActivity";
private static String DATABASE_NAME = "20180720";
private static String TABLE_NAME = "dayList";
private static int DATABASE_VERSION = 1;
private DatabaseHelper dbHelper;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (TextView) findViewById(R.id.status);
final EditText input01 = (EditText) findViewById(R.id.input01);
final EditText input02 = (EditText) findViewById(R.id.input02);
final EditText input03 = (EditText) findViewById(R.id.input03);
Button queryBtn = (Button) findViewById(R.id.queryBtn);
queryBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
boolean isOpen = openDatabase();
if (isOpen) {
executeRawQuery();
executeRawQueryParam2();
}
}
});
Button addBtn = (Button) findViewById(R.id.addBtn);
addBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name = input01.getText().toString();
int age = Integer.parseInt(input02.getText().toString());
String phone = input03.getText().toString();
String sqlInsert = "insert into dayList (name, age, phone) values (" +
"'" + name + "', " + age + " ,'" + phone + "');";
db.execSQL(sqlInsert);
}
});
}
private boolean openDatabase() {
println("opening database [" + DATABASE_NAME + "].");
dbHelper = new DatabaseHelper(this);
db = dbHelper.getWritableDatabase();
return true;
}
private void executeRawQuery() {
println("nexecuteRawQuery called.n");
Cursor c1 = db.rawQuery("select count(*) as Total from " + TABLE_NAME, null);
println("cursor count : " + c1.getCount());
c1.moveToNext();
println("record count : " + c1.getInt(0));
c1.close();
}
private void executeRawQueryParam() {
println("nexecuteRawQueryParam called.n");
String SQL = "select name, age, phone "
+ " from " + TABLE_NAME
+ " where age > ?";
String args= {"30"};
Cursor c1 = db.rawQuery(SQL, args);
int recordCount = c1.getCount();
println("cursor count : " + recordCount + "n");
for (int i = 0; i < recordCount; i++) {
c1.moveToNext();
String name = c1.getString(0);
int age = c1.getInt(1);
String phone = c1.getString(2);
println("Record #" + i + " : " + name + ", " + age + ", " + phone);
}
c1.close();
}
private void executeRawQueryParam2() {
println("nexecuteRawQueryParam2 called.n");
String columns = {"name", "age", "phone"};
Cursor c1 = db.query(TABLE_NAME, columns,
null, null,
null, null, null);
int recordCount = c1.getCount();
println("cursor count : " + recordCount + "n");
for (int i = 0; i < recordCount; i++) {
c1.moveToNext();
String name = c1.getString(0);
int age = c1.getInt(1);
String phone = c1.getString(2);
println("Record #" + i + " : " + name + ", " + age + ", " + phone);
}
c1.close();
}
private void println(String msg) {
Log.d(TAG, msg);
status.append("n" + msg);
}
private class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
println("creating table [" + TABLE_NAME + "].");
try {
String DROP_SQL = "drop table if exists " + TABLE_NAME;
db.execSQL(DROP_SQL);
} catch(Exception ex) {
Log.e(TAG, "Exception in DROP_SQL", ex);
}
String CREATE_SQL = "create table " + TABLE_NAME + "("
+ " _id integer PRIMARY KEY autoincrement, "
+ " name text, "
+ " age integer, "
+ " phone text)";
try {
db.execSQL(CREATE_SQL);
} catch(Exception ex) {
Log.e(TAG, "Exception in CREATE_SQL", ex);
}
println("inserting records.");
try {
db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('John', 20, '010-7788-1234');" );
db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('Mike', 35, '010-8888-1111');" );
db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('Sean', 26, '010-6677-4321');" );
} catch(Exception ex) {
Log.e(TAG, "Exception in insert SQL", ex);
}
}
public void onOpen(SQLiteDatabase db) {
println("opened database [" + DATABASE_NAME + "].");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ".");
}
}
}
When I push the queryBtn, the app is working normally, outputting information (which already saved in database) to the textView.
but when I push addBtn button, the app has stopped.
what is the problem?
And how can I insert the information input by Edittiext to the database?
android-edittext
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am making a simple app. The app get data through Edittext and insert it in database and output data from database to textView.
public class MainActivity extends AppCompatActivity {
private TextView status;
public static final String TAG = "MainActivity";
private static String DATABASE_NAME = "20180720";
private static String TABLE_NAME = "dayList";
private static int DATABASE_VERSION = 1;
private DatabaseHelper dbHelper;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (TextView) findViewById(R.id.status);
final EditText input01 = (EditText) findViewById(R.id.input01);
final EditText input02 = (EditText) findViewById(R.id.input02);
final EditText input03 = (EditText) findViewById(R.id.input03);
Button queryBtn = (Button) findViewById(R.id.queryBtn);
queryBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
boolean isOpen = openDatabase();
if (isOpen) {
executeRawQuery();
executeRawQueryParam2();
}
}
});
Button addBtn = (Button) findViewById(R.id.addBtn);
addBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name = input01.getText().toString();
int age = Integer.parseInt(input02.getText().toString());
String phone = input03.getText().toString();
String sqlInsert = "insert into dayList (name, age, phone) values (" +
"'" + name + "', " + age + " ,'" + phone + "');";
db.execSQL(sqlInsert);
}
});
}
private boolean openDatabase() {
println("opening database [" + DATABASE_NAME + "].");
dbHelper = new DatabaseHelper(this);
db = dbHelper.getWritableDatabase();
return true;
}
private void executeRawQuery() {
println("nexecuteRawQuery called.n");
Cursor c1 = db.rawQuery("select count(*) as Total from " + TABLE_NAME, null);
println("cursor count : " + c1.getCount());
c1.moveToNext();
println("record count : " + c1.getInt(0));
c1.close();
}
private void executeRawQueryParam() {
println("nexecuteRawQueryParam called.n");
String SQL = "select name, age, phone "
+ " from " + TABLE_NAME
+ " where age > ?";
String args= {"30"};
Cursor c1 = db.rawQuery(SQL, args);
int recordCount = c1.getCount();
println("cursor count : " + recordCount + "n");
for (int i = 0; i < recordCount; i++) {
c1.moveToNext();
String name = c1.getString(0);
int age = c1.getInt(1);
String phone = c1.getString(2);
println("Record #" + i + " : " + name + ", " + age + ", " + phone);
}
c1.close();
}
private void executeRawQueryParam2() {
println("nexecuteRawQueryParam2 called.n");
String columns = {"name", "age", "phone"};
Cursor c1 = db.query(TABLE_NAME, columns,
null, null,
null, null, null);
int recordCount = c1.getCount();
println("cursor count : " + recordCount + "n");
for (int i = 0; i < recordCount; i++) {
c1.moveToNext();
String name = c1.getString(0);
int age = c1.getInt(1);
String phone = c1.getString(2);
println("Record #" + i + " : " + name + ", " + age + ", " + phone);
}
c1.close();
}
private void println(String msg) {
Log.d(TAG, msg);
status.append("n" + msg);
}
private class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
println("creating table [" + TABLE_NAME + "].");
try {
String DROP_SQL = "drop table if exists " + TABLE_NAME;
db.execSQL(DROP_SQL);
} catch(Exception ex) {
Log.e(TAG, "Exception in DROP_SQL", ex);
}
String CREATE_SQL = "create table " + TABLE_NAME + "("
+ " _id integer PRIMARY KEY autoincrement, "
+ " name text, "
+ " age integer, "
+ " phone text)";
try {
db.execSQL(CREATE_SQL);
} catch(Exception ex) {
Log.e(TAG, "Exception in CREATE_SQL", ex);
}
println("inserting records.");
try {
db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('John', 20, '010-7788-1234');" );
db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('Mike', 35, '010-8888-1111');" );
db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('Sean', 26, '010-6677-4321');" );
} catch(Exception ex) {
Log.e(TAG, "Exception in insert SQL", ex);
}
}
public void onOpen(SQLiteDatabase db) {
println("opened database [" + DATABASE_NAME + "].");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ".");
}
}
}
When I push the queryBtn, the app is working normally, outputting information (which already saved in database) to the textView.
but when I push addBtn button, the app has stopped.
what is the problem?
And how can I insert the information input by Edittiext to the database?
android-edittext
I am making a simple app. The app get data through Edittext and insert it in database and output data from database to textView.
public class MainActivity extends AppCompatActivity {
private TextView status;
public static final String TAG = "MainActivity";
private static String DATABASE_NAME = "20180720";
private static String TABLE_NAME = "dayList";
private static int DATABASE_VERSION = 1;
private DatabaseHelper dbHelper;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (TextView) findViewById(R.id.status);
final EditText input01 = (EditText) findViewById(R.id.input01);
final EditText input02 = (EditText) findViewById(R.id.input02);
final EditText input03 = (EditText) findViewById(R.id.input03);
Button queryBtn = (Button) findViewById(R.id.queryBtn);
queryBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
boolean isOpen = openDatabase();
if (isOpen) {
executeRawQuery();
executeRawQueryParam2();
}
}
});
Button addBtn = (Button) findViewById(R.id.addBtn);
addBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name = input01.getText().toString();
int age = Integer.parseInt(input02.getText().toString());
String phone = input03.getText().toString();
String sqlInsert = "insert into dayList (name, age, phone) values (" +
"'" + name + "', " + age + " ,'" + phone + "');";
db.execSQL(sqlInsert);
}
});
}
private boolean openDatabase() {
println("opening database [" + DATABASE_NAME + "].");
dbHelper = new DatabaseHelper(this);
db = dbHelper.getWritableDatabase();
return true;
}
private void executeRawQuery() {
println("nexecuteRawQuery called.n");
Cursor c1 = db.rawQuery("select count(*) as Total from " + TABLE_NAME, null);
println("cursor count : " + c1.getCount());
c1.moveToNext();
println("record count : " + c1.getInt(0));
c1.close();
}
private void executeRawQueryParam() {
println("nexecuteRawQueryParam called.n");
String SQL = "select name, age, phone "
+ " from " + TABLE_NAME
+ " where age > ?";
String args= {"30"};
Cursor c1 = db.rawQuery(SQL, args);
int recordCount = c1.getCount();
println("cursor count : " + recordCount + "n");
for (int i = 0; i < recordCount; i++) {
c1.moveToNext();
String name = c1.getString(0);
int age = c1.getInt(1);
String phone = c1.getString(2);
println("Record #" + i + " : " + name + ", " + age + ", " + phone);
}
c1.close();
}
private void executeRawQueryParam2() {
println("nexecuteRawQueryParam2 called.n");
String columns = {"name", "age", "phone"};
Cursor c1 = db.query(TABLE_NAME, columns,
null, null,
null, null, null);
int recordCount = c1.getCount();
println("cursor count : " + recordCount + "n");
for (int i = 0; i < recordCount; i++) {
c1.moveToNext();
String name = c1.getString(0);
int age = c1.getInt(1);
String phone = c1.getString(2);
println("Record #" + i + " : " + name + ", " + age + ", " + phone);
}
c1.close();
}
private void println(String msg) {
Log.d(TAG, msg);
status.append("n" + msg);
}
private class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
println("creating table [" + TABLE_NAME + "].");
try {
String DROP_SQL = "drop table if exists " + TABLE_NAME;
db.execSQL(DROP_SQL);
} catch(Exception ex) {
Log.e(TAG, "Exception in DROP_SQL", ex);
}
String CREATE_SQL = "create table " + TABLE_NAME + "("
+ " _id integer PRIMARY KEY autoincrement, "
+ " name text, "
+ " age integer, "
+ " phone text)";
try {
db.execSQL(CREATE_SQL);
} catch(Exception ex) {
Log.e(TAG, "Exception in CREATE_SQL", ex);
}
println("inserting records.");
try {
db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('John', 20, '010-7788-1234');" );
db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('Mike', 35, '010-8888-1111');" );
db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('Sean', 26, '010-6677-4321');" );
} catch(Exception ex) {
Log.e(TAG, "Exception in insert SQL", ex);
}
}
public void onOpen(SQLiteDatabase db) {
println("opened database [" + DATABASE_NAME + "].");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ".");
}
}
}
When I push the queryBtn, the app is working normally, outputting information (which already saved in database) to the textView.
but when I push addBtn button, the app has stopped.
what is the problem?
And how can I insert the information input by Edittiext to the database?
android-edittext
android-edittext
asked Nov 22 at 14:04
User9728
64
64
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432697%2fthe-app-does-not-working-after-add-android-database-insert-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown