最近在编写关于成绩录入的一个程序,需要在一个数据库myTest.db里创建两个表,一个是学生信息表t_users,另一个是学生成绩表t_cj,学生需要根据学生信息表进行登陆,教师将成绩录入学生成绩表。但是程序总有问题,将成绩输入好单击保存就是停止运行,想请大神帮帮忙,看问题出在哪!
下面程序是DBHelper.java程序,创建数据库和两个表的程序
package com.bdu.managesys;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;
public class DBHelper extends SQLiteOpenHelper{
private static final int DB_VERSION =6;
private static final String TAG=“DBHelper”;
private static final String DB_NAME =“myTest.db”;
private static final String TABLE_NAME =“t_users”;
private static final String TABLE_NAME1 =“t_cj”;
public DBHelper(Context context, String name, CursorFactory factory, int version)
{
super(context, name, factory, version);
}
public DBHelper(Context context){
this(context,DB_NAME,null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.v(TAG, "onCreate");
db.execSQL( "Create Table If Not Exists " +
TABLE_NAME +
"(id text Primary Key,"//id
+ " userName text,"//用户名
+ " passWord text,"//密码
+ " userNum text,"//学号
+ " className text,"//班级名称
+ " userType text"//用户类型0是教师,1是学生。
+ ")");
db.execSQL( "Create Table If Not Exists " +
TABLE_NAME1 +
"(id text Primary Key,"//id
+ " userName text,"//用户名
+ " userNum text,"//学号
+ " className text,"//班级名称
+ " math text"//数学成绩。
+ " chinese text"//语文成绩。
+ " english text"//英语成绩。
+ ")");
}
@Override
public void onUpgrade(SQLiteDatabase db,
int oldVersion, int newVersion) {
Log.v(TAG, "onUpgrade");
}
}
下面是成绩录入CjActivity.java的程序:
package com.bdu.managesys;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import student.Student;
import studentdbhelper.StudentDBHelper;
import tablecontanst.TableContanst;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class CjActivity extends Activity implements OnClickListener {
private TextView idText;
private EditText nameText;
private EditText xuehaoText;
private EditText banjiText;
private EditText mathText;
private EditText chineseText;
private EditText englishText;
private Button restoreButton;
private String sex;
private EditText dataText;
private Button resetButton;
private DBHelper dbh;
private SQLiteDatabase sqldb;
private SQLiteDatabase db;
private Context ctx;
private Intent intent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cj_layout);
resetButton = (Button) findViewById(R.id.btn_clear);
resetButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();//这个是关键
}
});
initView();
dbh = new DBHelper(this);
sqldb= dbh.getWritableDatabase();
}
public void initView(){
idText = (TextView) findViewById(R.id.tv_stu_id);
nameText = (EditText) findViewById(R.id.et_name);
xuehaoText = (EditText) findViewById(R.id.et_xuehao);
banjiText = (EditText) findViewById(R.id.et_banji);
mathText = (EditText) findViewById(R.id.et_math);
chineseText = (EditText) findViewById(R.id.et_chinese);
englishText = (EditText) findViewById(R.id.et_english);
restoreButton = (Button) findViewById(R.id.btn_save);
resetButton = (Button) findViewById(R.id.btn_clear);
}
public void bc(View v) {
//bc是保存按钮的onClick方法
String userName = nameText.getText().toString();
String xuehao = xuehaoText.getText().toString();
String banji =banjiText.getText().toString();
String math = mathText.getText().toString();
String chinese = chineseText.getText().toString();
String english = englishText.getText().toString();
Date d = new Date();
long id = d.getTime();
String sql = "Insert Into t_cj"
+ "(id,userName,userNum,className,math,chinese,english) "
+ "values("+id+",'"+userName+"','"+xuehao+"',"
+ "'"+banji+"','"+math+"','"+chinese+"','"+english+"')";
sqldb.execSQL(sql.toString());
Log.e("sql语句", sql);
Toast.makeText(this,"录入成功", Toast.LENGTH_LONG).show();
sqldb.close();//关闭数据库对象
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}