问题描述
- 安卓sqlite的问题,不能点注册按钮,点了就出错。
- 安卓sqlite的问题,不能点注册按钮,点了就出错。然后控制台那边提示说是such no table,网上也查找过解决办法,说是把路径定死,可惜不知道哪里写合适,或者试了也不行?
注册java的代码
import java.io.File;import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;public class ZhuceActivity extends Activity {
private EditText edityhmedityhmmedityhxmedityhsfzedityhdhedityhyx;
private RadioButton rdmanrdwomen;
private RadioGroup rdgroup;
private Button yhzhuce;
private Button yhfanhui;
SQLiteDatabase db;
// 声明一个变量zhuceOK。布尔类型
private boolean zhuceok = false;
Handler handler;@Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); db.close(); //不在当前页时会注销此页面 } @Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.zhuce); //获取控件 edityhm = (EditText)findViewById(R.id.edityhm); edityhmm = (EditText)findViewById(R.id.edityhmm); edityhxm = (EditText)findViewById(R.id.edityhxm); edityhsfz = (EditText)findViewById(R.id.edityhsfz); edityhyx = (EditText)findViewById(R.id.edityhyx); edityhdh = (EditText)findViewById(R.id.edityhdh); rdman = (RadioButton) findViewById(R.id.rdman); rdwomen = (RadioButton) findViewById(R.id.rdwomen); rdgroup = (RadioGroup)findViewById(R.id.rdgroup); yhzhuce = (Button)findViewById(R.id.yhzhuce); yhfanhui = (Button)findViewById(R.id.yhfanhui); // 添加监听 yhzhuce.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO 自动生成的方法存根 // 获取所有的用户信息,访问网络服务器端,将信息发送给服务器端去处理。 User user = new User(); user.setName(edityhm.getText().toString()); user.setPassword(edityhmm.getText().toString()); user.setYhsfz(edityhsfz.getText().toString()); user.setYhxm(edityhxm.getText().toString()); user.setYhyx(edityhyx.getText().toString()); user.setYhdh(edityhdh.getText().toString()); if (rdman.isChecked()) user.setSex(""男""); else user.setSex(""女""); if (zhuce(user)) {// 调用zhuce方法,如果注册成功 // 跳转到登录界面 Intent in = new Intent(); in.setClass(ZhuceActivity.thisLoginActivity.class); startActivity(in); // 销毁当前activity ZhuceActivity.this.onDestroy(); } else {// 注册不成功 Toast.makeText(ZhuceActivity.thissorry!!,注册失败"" Toast.LENGTH_SHORT).show(); edityhm.requestFocus(); } }}); yhfanhui.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent in1 = new Intent(); in1.setClass(ZhuceActivity.thisLoginActivity.class); ZhuceActivity.this.startActivity(in1); } }); }
// 添加用户
// User user这段语句是直接
public Boolean zhuce( User user) {Boolean zhuceok = false;
String str = ""insert into user values(???????) "";LoginActivity main = new LoginActivity();
db=SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString() +""/test.dbs""null);
main.db = db;try {
db.execSQL(str new String[] { user.getName()user.getPassword()user.getYhsfz()user.getYhxm()
user.getYhyx()user.getYhdh()user.getSex()});zhuceok = true;
} catch (Exception e) {
main.createDb();
return zhuceok;
}return zhuceok;
}
}
登录java代码
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;public class LoginActivity extends Activity {
// 声明控件变量 注意变量为null时能
private EditText login_username;
private EditText login_password;
private Button zhuce;
private Button denglu;
public static SQLiteDatabase db;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); login_username = (EditText)findViewById(R.id.login_username); login_password = (EditText)findViewById(R.id.login_password); zhuce = (Button)findViewById(R.id.zhuce); denglu = (Button)findViewById(R.id.denglu); zhuce.setOnClickListener(new zhuceListener()); denglu.setOnClickListener(new dengluListener()); db = SQLiteDatabase.openOrCreateDatabase(LoginActivity.this.getFilesDir().toString() + ""/test.dbs"" null); //getFilesDir().toString()是获取存放数据库文件的路径.}//添加一个监听器
class zhuceListener implements OnClickListener{
@Overridepublic void onClick(View arg0) { // TODO 自动生成的方法存根 //生成一个Intent对象 Intent intent = new Intent(); intent.setClass(LoginActivity.this ZhuceActivity.class); LoginActivity.this.startActivity(intent);}
}
class dengluListener implements OnClickListener{@Overridepublic void onClick(View arg0) { // TODO 自动生成的方法存根 String name = login_username.getText().toString(); String password = login_password.getText().toString(); if (name.equals("""") || password.equals("""")) { // 弹出消息框 AlertDialog 通过Builder方法,然后用show()来实现 new AlertDialog.Builder(LoginActivity.this).setTitle(""错误"") .setMessage(""帐号或密码不能空"").setPositiveButton(""确定"" null) .show(); } else { isUserinfo(name password); } }
}
// 判断输入的用户是否正确
public Boolean isUserinfo(String name String pwd) {
try{
String str=""select * from user where name=? and password=?"";
Cursor cursor = db.rawQuery(str new String []{namepwd});
//Cursor 是每行的集合。SQLite 数据库,,,,db.rawQuery执行本地SQL语句查询
if(cursor.getCount()<=0){new AlertDialog.Builder(LoginActivity.this).setTitle(""错误"")
.setMessage(""帐号或密码错误!"").setPositiveButton(""确定"" null)
.show();
return false;
}else{
new AlertDialog.Builder(LoginActivity.this).setTitle(""正确"")
.setMessage(""成功登录"").setPositiveButton(""确定""new DialogInterface.OnClickListener(){
//设置确定按钮点击的事件Listener可以直接在这边设置,纠结了好久
@Override
public void onClick(DialogInterface dialog int which) {
//实现你的onclick方法
Intent it = new Intent(LoginActivity.thisJiemianActivity.class); startActivity(it);
}}
).
show();return true;
}
}catch(SQLiteException e){ createDb(); } return false;
}
// 创建数据库和用户表
public void createDb() {
db.execSQL(""create table user( name varchar(30) primary keypassword varchar(30)yhsfz varchar(50) +
yhxm varchar(30)yhyx varchar(50)yhdh varchar(50)sex varchar(20)"");}
//举例如果你触发startButton按钮事件启动线程,而没有触发stopButton按钮事件停止线程,在离开该页面时onDestroy()中的停止线程功能就发挥了作用
protected void onDestroy() {// TODO Auto-generated method stub
super.onDestroy();
db.close();
}
@Overridepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.login menu); return true;}
}
还有就是声明user对象的属性
public class User {
private String name;
private String password;
private String yhsfz;
private String yhxm;
private String yhyx;
private String yhdh;
private String sex;public String getName(){ return name;}public void setName(String name){ this.name = name;}public String getPassword(){ return name;}public void setPassword(String password){ this.password = password;}public String getYhsfz(){ return yhsfz;}public void setYhsfz(String yhsfz){ this.yhsfz = yhsfz;}public String getYhxm(){ return yhxm;}public void setYhxm(String yhxm){ this.yhxm = yhxm;}public String getYhyx(){ return yhyx;}public void setYhyx(String yhyx){ this.yhyx = yhyx;}public String getYhdh(){ return yhdh;}public void setYhdh(String yhdh){ this.yhdh = yhdh;}public String getSex(){ return sex;}public void setSex(String sex){ this.sex = sex;} 不知道该如何解决,可能是哪里的代码写错了,望各路大神能指点下,感谢!
解决方案
such no table 你看看你的数据库中有没有表啊,你的表都没有生成成功,。