| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 592 人关注过本帖
标题:大哥大姐帮忙解决一下,运行时总是说密码错误,挑不出来
取消只看楼主 加入收藏
王洋爱135
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2012-8-15
结帖率:0
收藏
已结贴  问题点数:10 回复次数:2 
大哥大姐帮忙解决一下,运行时总是说密码错误,挑不出来
数据库代码:
package

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

class Sql extends SQLiteOpenHelper{
   
    //创建用户表
    String createUserTable = "create table user_info(_id int auto_increment,username char(20),"
        + "password char(20),primary key('_id'));";
   
    // 定义用户表插入语句
    String insertStr = "insert into user_info(_id,username,password) values(?,?,?)";

    public Sql(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name,factory , version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建用户表,
        db.execSQL(createUserTable);
        
        // 插入测试data
        /*String[] insertValue = { "0", "admin", "admin" };
        db.execSQL(insertStr, insertValue);*/
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        
    }
   
}

注册代码:package

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

public class AssistantRegister extends Activity
{
    static EditText userName01;
    static EditText password01;
    Sql db;
    SQLiteDatabase sDatabase = null;
    public void close()
    {
        /*if(mCursor!=null){
            mCursor.close();
        }*/
        sDatabase.close();
    }
    private ImageButton dpress;
    private ImageButton dpress2;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        
            super.onCreate(savedInstanceState);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.register);
            
            userName01 = (EditText)findViewById(R.id.text3);
            password01 = (EditText)findViewById(R.id.text4);
            this.dpress=(ImageButton)findViewById(R.id.Click_Button12);
            this.dpress2=(ImageButton)findViewById(R.id.Click_Button13);
      
            dpress.setOnClickListener(new android.view.View.OnClickListener()
            {
            public void onClick(View v)
            {
                db = new Sql(getApplicationContext(), "store.db", null, 1);
                sDatabase = db.getWritableDatabase();
                if (userName01.getText().toString().equals("")
                                || password01.getText().toString().equals(""))
                {

                            DialogDemo.builder(AssistantRegister.this, "错误信息", "请填写完整信息!");

                        
                }
                else
                {
                    int id1=0;
                    String select = "select max(_id) from user_info";
                    Cursor seCursor = sDatabase.rawQuery(select, null);
                    seCursor.moveToFirst();
                    id1 = Integer.parseInt(seCursor.getString(0));
                    id1 += 1;
                    sDatabase.execSQL("insert into user_info values('" + id1 + "','"
                                            + userName01 + "','" + password01 + "')"
                                            );
                    DialogDemo.builder(AssistantRegister.this, "提示", "注册成功,请返回登录界面登录");
                                    //seCursor.close();
                                    sDatabase.close();
                 
                    
                }
                        
                        
            }
        } );
        dpress2.setOnClickListener(new android.view.View.OnClickListener()
        {
            public void onClick(View v)
            {
                /* 新建一个Intent对象 */
                Intent intent = new Intent();
                /* 指定intent要启动的类 */
                intent.setClass(AssistantRegister.this,AssistantLoadActivity.class);
                /* 启动一个新的Activity */
                startActivity(intent);
                /* 关闭当前的Activity */
                AssistantRegister.this.finish();
               
            }
            
        } );
   
    }
   
}

登陆代码:
package

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class AssistantLoad extends Activity
{
    private ImageButton dpress;
    private ImageButton dpress2;
    private EditText use,password;
    Sql db;
    SQLiteDatabase sDatabase = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.denglu);
        use = (EditText)findViewById(R.id.text1);
        password = (EditText)findViewById(R.id.text2);
       this.dpress=(ImageButton)findViewById(R.id.Click_Button8);
       this.dpress2=(ImageButton)findViewById(R.id.Click_Button9);
       db = new Sql(getApplicationContext(), "store.db", null, 1);
        sDatabase = db.getWritableDatabase();
        dpress.setOnClickListener(new android.view.View.OnClickListener()
        {
            public void onClick(View v)
            {
                String i = use.getText().toString();
                //String i1 = password.getText().toString();
                String userName = "";
                String userPw = "";
                //编写数据库语句
                String select_sql = "select username,password from user_info where username = '"
                        + i + "'";
                //执行语句
                Cursor cursor = sDatabase.rawQuery(select_sql, null);
                if(cursor!=null){
                cursor.moveToFirst();
               
                try
                {
                    
                    userName = cursor.getString(1);
                    userPw = cursor.getString(2);
                    
                }
                catch (Exception e)
                {
                    // TODO: handle exception
                    userName = "";
                    userPw = "";
                }}
//                判断用户名是否为空
                if (i.equals(""))
                {
                    DialogDemo.builder(AssistantLoad.this, "错误信息",
                            "用户名不能为空!");
                }
//                判断密码是否为空
                else if (password.getText().toString().equals(""))
                {
                    DialogDemo.builder(AssistantLoad.this, "错误信息",
                            "密码不能为空!");
                }
               
//                判断用户名和密码是否正确
                else if (!(use.getText().toString().equals(userName) && password
                        .getText().toString().equals(userPw)))
                {
                    DialogDemo.builder(AssistantLoad.this, "错误信息",
                            "用户名或密码错误,请重新输入");
                }
//                全部正确跳转到操作界面
                else
                {
                    cursor.close();
                    Intent intent = new Intent();
                    //Bundle bundle = new Bundle();
                    //bundle.putString("username",userName);
                    //intent.putExtras(bundle);
                    intent.setClass(AssistantLoad.this, Assistant.class);
                    startActivity(intent);
                    
                    Toast.makeText(AssistantLoad.this, "登陆成功", Toast.LENGTH_SHORT).show();
                    

                }
               
            }
            
        } );
        dpress2.setOnClickListener(new android.view.View.OnClickListener()
        {
            public void onClick(View v)
            {
                /* 新建一个Intent对象 */
                Intent intent = new Intent();
                /* 指定intent要启动的类 */
                intent.setClass(AssistantLoad.this,AssistantLoadActivity.class);
                /* 启动一个新的Activity */
                startActivity(intent);
                /* 关闭当前的Activity */
                AssistantLoad.this.finish();
            }
            
        } );
   
    }
   
}
搜索更多相关主题的帖子: primary password package 
2012-08-15 11:06
王洋爱135
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2012-8-15
收藏
得分:0 
主要是有个地方错了但我不会改,userName = cursor.getString(1);
                    userPw = cursor.getString(2);这句话有问题在登陆代码中,获得存入数据库的数据的时候出错,不知道怎么改正
2012-08-16 16:05
王洋爱135
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2012-8-15
收藏
得分:0 
回复 2楼 netlin
主要是有个地方错了但我不会改,userName = cursor.getString(1);
                    userPw = cursor.getString(2);这句话有问题在登陆代码中,获得存入数据库的数据的时候出错,不知道怎么改正
2012-08-16 17:24
快速回复:大哥大姐帮忙解决一下,运行时总是说密码错误,挑不出来
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020051 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved